Apr-18-2019, 10:47 AM
(Apr-15-2019, 08:42 PM)Truman Wrote: is j value and i key:value pair in your code?
You can easily test what is what
In [1]: from itertools import combinations In [2]: d = {'Maggie': 3, 'Herman': 7, 'Betsy': 9, 'Oreo': 6, 'Moo Moo': 3, 'Milkshake': 2, ...: 'Millie': 5, 'Lola': 2, 'Florence': 2, 'Henrietta': 9} In [3]: list(combinations(d, 2))[:5] # first five combinations to see what we get Out[3]: [('Maggie', 'Herman'), ('Maggie', 'Betsy'), ('Maggie', 'Oreo'), ('Maggie', 'Moo Moo'), ('Maggie', 'Milkshake')]We get key pair combinations, but they don't have values. With this part we 'put values back and convert to dict':
{j: d[j] for j in i}
- from each pair we take individual keys and construct new dictionary by getting key and its corresponding value from original dictionary.For first row in pair combinations
('Maggie', 'Herman')
:{j: d[j] for j in i} --> {'Maggie': d['Maggie'], 'Herman': d['Herman']} --> {'Maggie': 3, 'Herman': 7}For sake of readabilty the
d[j]
could be replaced with d.get(j)
- this gives better understanding that we will 'get' value of j.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.