Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #84930 > unrolled thread
| Started by | rajanbond@gmail.com |
|---|---|
| First post | 2015-01-30 18:27 -0800 |
| Last post | 2015-02-01 11:51 -0700 |
| Articles | 15 — 8 participants |
Back to article view | Back to comp.lang.python
Create dictionary based of x items per key from two lists rajanbond@gmail.com - 2015-01-30 18:27 -0800
Re: Create dictionary based of x items per key from two lists Chris Angelico <rosuav@gmail.com> - 2015-01-31 13:38 +1100
Re: Create dictionary based of x items per key from two lists Jason Friedman <jsf80238@gmail.com> - 2015-01-31 19:38 -0700
Re: Create dictionary based of x items per key from two lists rajanbond@gmail.com - 2015-02-02 10:48 -0800
Re: Create dictionary based of x items per key from two lists rajanbond@gmail.com - 2015-02-02 11:00 -0800
Re: Create dictionary based of x items per key from two lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-01 03:06 +0000
Re: Create dictionary based of x items per key from two lists Chris Angelico <rosuav@gmail.com> - 2015-02-01 14:22 +1100
Re: Create dictionary based of x items per key from two lists Grant Edwards <invalid@invalid.invalid> - 2015-02-01 18:14 +0000
Re: Create dictionary based of x items per key from two lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-01 18:49 +0000
Re: Create dictionary based of x items per key from two lists mm0fmf <none@mailinator.com> - 2015-02-01 18:52 +0000
Re: Create dictionary based of x items per key from two lists Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-01 12:36 -0700
Re: Create dictionary based of x items per key from two lists wxjmfauth@gmail.com - 2015-02-02 23:13 -0800
Re: Create dictionary based of x items per key from two lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-01 20:28 +0000
Re: Create dictionary based of x items per key from two lists Chris Angelico <rosuav@gmail.com> - 2015-02-02 07:33 +1100
Re: Create dictionary based of x items per key from two lists Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-01 11:51 -0700
| From | rajanbond@gmail.com |
|---|---|
| Date | 2015-01-30 18:27 -0800 |
| Subject | Create dictionary based of x items per key from two lists |
| Message-ID | <0dddee06-233b-436a-be48-3c16e62c1718@googlegroups.com> |
I have two lists
l1 = ["a","b","c","d","e","f","g","h","i","j"]
l2 = ["aR","bR","cR"]
l2 will always be smaller or equal to l1
numL1PerL2 = len(l1)/len(l2)
I want to create a dictionary that has key from l1 and value from l2 based on numL1PerL2
So
{
a:aR,
b:aR,
c:aR,
d:bR,
e:bR,
f:bR,
g:cR,
h:cR,
i:cR,
j:cR
}
So last item from l2 is key for remaining items from l1
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-01-31 13:38 +1100 |
| Message-ID | <mailman.18327.1422671928.18130.python-list@python.org> |
| In reply to | #84930 |
On Sat, Jan 31, 2015 at 1:27 PM, <rajanbond@gmail.com> wrote:
> l1 = ["a","b","c","d","e","f","g","h","i","j"]
> l2 = ["aR","bR","cR"]
>
> l2 will always be smaller or equal to l1
>
> numL1PerL2 = len(l1)/len(l2)
>
> I want to create a dictionary that has key from l1 and value from l2 based on numL1PerL2
>
> So
>
> {
> a:aR,
> b:aR,
> c:aR,
> d:bR,
> e:bR,
> f:bR,
> g:cR,
> h:cR,
> i:cR,
> j:cR
> }
>
> So last item from l2 is key for remaining items from l1
So the Nth element of l1 will always be paired with the
(N/numL1PerL2)th element of l2 (with the check at the end)? Seems easy
enough.
dups = len(l1)/len(l2)
l2.append(l2[-1])
result = {x:l2[i/dups] for i,x in enumerate(l1)}
This mutates l2 for convenience, but you could also adjust the index
to take care of the excess. As a one-liner:
result = {x:l2[min(i/(len(l1)/len(l2)),len(l2)-1)] for i,x in enumerate(l1)}
But the one-liner is not better code :)
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Jason Friedman <jsf80238@gmail.com> |
|---|---|
| Date | 2015-01-31 19:38 -0700 |
| Message-ID | <mailman.18344.1422758329.18130.python-list@python.org> |
| In reply to | #84930 |
> I have two lists
>
> l1 = ["a","b","c","d","e","f","g","h","i","j"]
> l2 = ["aR","bR","cR"]
>
> l2 will always be smaller or equal to l1
>
> numL1PerL2 = len(l1)/len(l2)
>
> I want to create a dictionary that has key from l1 and value from l2 based on numL1PerL2
>
> So
>
> {
> a:aR,
> b:aR,
> c:aR,
> d:bR,
> e:bR,
> f:bR,
> g:cR,
> h:cR,
> i:cR,
> j:cR
> }
Another possibility is:
import itertools
my_dict = {x:y for x,y in zip(list1, itertools.cycle(list2))}
[toc] | [prev] | [next] | [standalone]
| From | rajanbond@gmail.com |
|---|---|
| Date | 2015-02-02 10:48 -0800 |
| Message-ID | <fe62abdb-bd6b-41f5-88fa-192a47a4cd66@googlegroups.com> |
| In reply to | #84971 |
On Saturday, 31 January 2015 18:39:01 UTC-8, Jason Friedman wrote:
> > I have two lists
> >
> > l1 = ["a","b","c","d","e","f","g","h","i","j"]
> > l2 = ["aR","bR","cR"]
> >
> > l2 will always be smaller or equal to l1
> >
> > numL1PerL2 = len(l1)/len(l2)
> >
> > I want to create a dictionary that has key from l1 and value from l2 based on numL1PerL2
> >
> > So
> >
> > {
> > a:aR,
> > b:aR,
> > c:aR,
> > d:bR,
> > e:bR,
> > f:bR,
> > g:cR,
> > h:cR,
> > i:cR,
> > j:cR
> > }
>
> Another possibility is:
> import itertools
> my_dict = {x:y for x,y in zip(list1, itertools.cycle(list2))}
NO. Sorry if this was not very clear.
In teh above example- len(l1) = 10
len(l2) = 3
So, the dict can not have more than 3 keys from l1 with same value from l2 except the last element from l1 .
So
a,b,c will have one key- say aR
d,e,f - bR
g,h,i,j- cR- j has key cR because the number l1 is not completely divisible by l2 and leave a remainder.
[toc] | [prev] | [next] | [standalone]
| From | rajanbond@gmail.com |
|---|---|
| Date | 2015-02-02 11:00 -0800 |
| Message-ID | <8c614bc1-4497-4406-8fb2-0c0034defd42@googlegroups.com> |
| In reply to | #84971 |
On Saturday, 31 January 2015 18:39:01 UTC-8, Jason Friedman wrote:
> > I have two lists
> >
> > l1 = ["a","b","c","d","e","f","g","h","i","j"]
> > l2 = ["aR","bR","cR"]
> >
> > l2 will always be smaller or equal to l1
> >
> > numL1PerL2 = len(l1)/len(l2)
> >
> > I want to create a dictionary that has key from l1 and value from l2 based on numL1PerL2
> >
> > So
> >
> > {
> > a:aR,
> > b:aR,
> > c:aR,
> > d:bR,
> > e:bR,
> > f:bR,
> > g:cR,
> > h:cR,
> > i:cR,
> > j:cR
> > }
>
> Another possibility is:
> import itertools
> my_dict = {x:y for x,y in zip(list1, itertools.cycle(list2))}
Thank you Jason! Looks like this will work for my case.
I had a solution working with count and num to keep track of the number of times l2 items are assigned as value but was looking for a more pythonic way.
count = 0
num = 0
map = {}
# for item in l2:
# while count < numL1PerL2 and num <= len(l1):
# map[l1[num]] = item
# count += 1
# num += 1
# count = 0
# map[l1[num]] = l2[-1]
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-02-01 03:06 +0000 |
| Message-ID | <mailman.18345.1422759990.18130.python-list@python.org> |
| In reply to | #84930 |
On 31/01/2015 02:38, Chris Angelico wrote:
> On Sat, Jan 31, 2015 at 1:27 PM, <rajanbond@gmail.com> wrote:
>> l1 = ["a","b","c","d","e","f","g","h","i","j"]
>> l2 = ["aR","bR","cR"]
>>
>> l2 will always be smaller or equal to l1
>>
>> numL1PerL2 = len(l1)/len(l2)
>>
>> I want to create a dictionary that has key from l1 and value from l2 based on numL1PerL2
>>
>> So
>>
>> {
>> a:aR,
>> b:aR,
>> c:aR,
>> d:bR,
>> e:bR,
>> f:bR,
>> g:cR,
>> h:cR,
>> i:cR,
>> j:cR
>> }
>>
>> So last item from l2 is key for remaining items from l1
>
> So the Nth element of l1 will always be paired with the
> (N/numL1PerL2)th element of l2 (with the check at the end)? Seems easy
> enough.
>
> dups = len(l1)/len(l2)
> l2.append(l2[-1])
> result = {x:l2[i/dups] for i,x in enumerate(l1)}
>
> This mutates l2 for convenience, but you could also adjust the index
> to take care of the excess. As a one-liner:
>
> result = {x:l2[min(i/(len(l1)/len(l2)),len(l2)-1)] for i,x in enumerate(l1)}
>
> But the one-liner is not better code :)
>
> ChrisA
>
The one-liner might not be better code, but it must be better speed wise
precisely because it's on one line, right? :)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-02-01 14:22 +1100 |
| Message-ID | <mailman.18347.1422760944.18130.python-list@python.org> |
| In reply to | #84930 |
On Sun, Feb 1, 2015 at 2:06 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: > The one-liner might not be better code, but it must be better speed wise > precisely because it's on one line, right? :) Well of course it is. Python code speed is always measured in lines per minute. That's why you should eliminate blank lines from your code. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2015-02-01 18:14 +0000 |
| Message-ID | <malqet$kf0$1@reader1.panix.com> |
| In reply to | #84977 |
On 2015-02-01, Chris Angelico <rosuav@gmail.com> wrote: > On Sun, Feb 1, 2015 at 2:06 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: >> The one-liner might not be better code, but it must be better speed wise >> precisely because it's on one line, right? :) > > Well of course it is. Python code speed is always measured in lines > per minute. That's why you should eliminate blank lines from your > code. No, you've got that backwards. You want _more_ blank lines. A blank line takes zero time to run, but it still counts as a line in your lines/second stats. A loop containing 1 line of code will execute in the same abount of time as that loop with 1 line of code and 99 blanks lines. The latter loop is running at 100 times as many lines/second as the former. That's _got_ to be better. -- Grant
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-02-01 18:49 +0000 |
| Message-ID | <mailman.18369.1422816602.18130.python-list@python.org> |
| In reply to | #85014 |
On 01/02/2015 18:14, Grant Edwards wrote: > On 2015-02-01, Chris Angelico <rosuav@gmail.com> wrote: >> On Sun, Feb 1, 2015 at 2:06 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: >>> The one-liner might not be better code, but it must be better speed wise >>> precisely because it's on one line, right? :) >> >> Well of course it is. Python code speed is always measured in lines >> per minute. That's why you should eliminate blank lines from your >> code. > > No, you've got that backwards. You want _more_ blank lines. A blank > line takes zero time to run, but it still counts as a line in your > lines/second stats. > > A loop containing 1 line of code will execute in the same abount of > time as that loop with 1 line of code and 99 blanks lines. > > The latter loop is running at 100 times as many lines/second as the > former. That's _got_ to be better. > At long last my quest seeking the final entry for the Zen of Python is over. I'll be delighted to let you accept the honour of raising an issue on the bug tracker to get the "this" module changed to reflect my new found wisdom. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | mm0fmf <none@mailinator.com> |
|---|---|
| Date | 2015-02-01 18:52 +0000 |
| Message-ID | <uBuzw.580414$Ud7.348525@fx11.am4> |
| In reply to | #85014 |
On 01/02/2015 18:14, Grant Edwards wrote: > No, you've got that backwards. You want_more_ blank lines. A blank > line takes zero time to run, but it still counts as a line in your > lines/second stats. You want more blanks lines to satisfy bean counting managers who want you to report "number of lines of code written per week". More blank lines equals happier managers! ;-)
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-02-01 12:36 -0700 |
| Message-ID | <mailman.18373.1422819423.18130.python-list@python.org> |
| In reply to | #85014 |
On Sun, Feb 1, 2015 at 11:49 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: > On 01/02/2015 18:14, Grant Edwards wrote: >> A loop containing 1 line of code will execute in the same abount of >> time as that loop with 1 line of code and 99 blanks lines. >> >> The latter loop is running at 100 times as many lines/second as the >> former. That's _got_ to be better. >> > > At long last my quest seeking the final entry for the Zen of Python is over. > I'll be delighted to let you accept the honour of raising an issue on the > bug tracker to get the "this" module changed to reflect my new found wisdom. And while you're at it, please also work on changing the name of the module to "self" -- "this" is for Java.
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2015-02-02 23:13 -0800 |
| Message-ID | <56384fa2-e2a7-430d-ab2d-e878bedaa15a@googlegroups.com> |
| In reply to | #85023 |
Le dimanche 1 février 2015 20:37:14 UTC+1, Ian a écrit : > On Sun, Feb 1, 2015 at 11:49 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: > > On 01/02/2015 18:14, Grant Edwards wrote: > >> A loop containing 1 line of code will execute in the same abount of > >> time as that loop with 1 line of code and 99 blanks lines. > >> > >> The latter loop is running at 100 times as many lines/second as the > >> former. That's _got_ to be better. > >> > > > > At long last my quest seeking the final entry for the Zen of Python is over. > > I'll be delighted to let you accept the honour of raising an issue on the > > bug tracker to get the "this" module changed to reflect my new found wisdom. > > And while you're at it, please also work on changing the name of the > module to "self" -- "this" is for Java. >>> class C: ... def __init__(carot, x): ... carot.x = 999 ... def p(carot): ... print(carot.x) ... >>> c = C(-1) >>> c.p() 999 >>> "self" is not a keyword. --- This will be very funny: age_of_the_captain = int def GetCommandantAge(i: age_of_the_captain) -> age_of_the_captain: Allowed? age_of_the_captain = int cardinal = int BosonSpin = cardinal # can be zero def GetCommanderAge(i: age_of_the_captain) -> BosonSpin: Propose a clean language, instead of blaming those who are writing bad code.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-02-01 20:28 +0000 |
| Message-ID | <mailman.18375.1422822531.18130.python-list@python.org> |
| In reply to | #85014 |
On 01/02/2015 19:36, Ian Kelly wrote: > On Sun, Feb 1, 2015 at 11:49 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: >> On 01/02/2015 18:14, Grant Edwards wrote: >>> A loop containing 1 line of code will execute in the same abount of >>> time as that loop with 1 line of code and 99 blanks lines. >>> >>> The latter loop is running at 100 times as many lines/second as the >>> former. That's _got_ to be better. >>> >> >> At long last my quest seeking the final entry for the Zen of Python is over. >> I'll be delighted to let you accept the honour of raising an issue on the >> bug tracker to get the "this" module changed to reflect my new found wisdom. > > And while you're at it, please also work on changing the name of the > module to "self" -- "this" is for Java. > I think that'll need a PEP :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-02-02 07:33 +1100 |
| Message-ID | <mailman.18377.1422822807.18130.python-list@python.org> |
| In reply to | #85014 |
On Mon, Feb 2, 2015 at 6:36 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote: >> At long last my quest seeking the final entry for the Zen of Python is over. >> I'll be delighted to let you accept the honour of raising an issue on the >> bug tracker to get the "this" module changed to reflect my new found wisdom. > > And while you're at it, please also work on changing the name of the > module to "self" -- "this" is for Java. I thought it was meant to be like English - "Here, import this". ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-02-01 11:51 -0700 |
| Message-ID | <mailman.18370.1422816759.18130.python-list@python.org> |
| In reply to | #84930 |
On Sat, Jan 31, 2015 at 7:38 PM, Jason Friedman <jsf80238@gmail.com> wrote:
>> I have two lists
>>
>> l1 = ["a","b","c","d","e","f","g","h","i","j"]
>> l2 = ["aR","bR","cR"]
>>
>> l2 will always be smaller or equal to l1
>>
>> numL1PerL2 = len(l1)/len(l2)
>>
>> I want to create a dictionary that has key from l1 and value from l2 based on numL1PerL2
>>
>> So
>>
>> {
>> a:aR,
>> b:aR,
>> c:aR,
>> d:bR,
>> e:bR,
>> f:bR,
>> g:cR,
>> h:cR,
>> i:cR,
>> j:cR
>> }
>
> Another possibility is:
> import itertools
> my_dict = {x:y for x,y in zip(list1, itertools.cycle(list2))}
That results in a different mapping than the one specified by the OP,
though. An alternate itertools approach could be:
dict(zip_longest(l1, chain.from_iterable(
map(partial(repeat, times=len(l1) // len(l2)), l2)), fillvalue=l2[-1]))
although this requires the inputs to be sequences, not arbitrary iterables.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web