On 31/01/2015 02:38, Chris Angelico wrote:
On Sat, Jan 31, 2015 at 1:27 PM,  <rajanb...@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

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to