Issue
I found this useful bit of code among the series of tubes that is the internet:
x=[1,2,3,4]
y=[1,2,3,4]
combos=[(`i`+`n`) for i in x for n in y]
combos
['11','12','13','14','21','22','23','24','31','32','33','34','41','42','43','44']
What I'm trying to do is something like the following:
combinations={(i: `n`+`d`) for i in range(16) for n in x for d in y}
combinations
{1: '11', 2: '12', 3: '13', 4: '14', 5: '21', 6: '22'...etc}
But obviously that's not working. Can this be done? If so, how?
Solution
combos = [str(i) + str(n) for i in x for n in y] # or `i`+`n`, () for a generator
combinations = dict((i+1,c) for i,c in enumerate(combos))
# Only in Python 2.6 and newer:
combinations = dict(enumerate(combos, 1))
# Only in Python 2.7 and newer:
combinations = {i+1:c for i,c in enumerate(combos)}
Answered By - phihag
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.