Converting tuple of lists of variable length into dictionary
Denis McMahon
denismfmcmahon at gmail.com
Sun Oct 18 16:38:26 EDT 2015
On Sun, 18 Oct 2015 03:17:18 -0700, Beppe wrote:
> hi to everybody, I must turn a tuple of lists into a dictionary.
I went down a different path to Peter, and discovered something
perplexing:
This failed:
d = { i: deepcopy(l).remove(i) for l in t for i in l }
So I tried to expand it out to see where the issue was, and ended up with
two variants:
def f1(l, i):
m = deepcopy(l)
m.remove(i)
return m
d = { i: f1(l,i) for l in t for i in l }
def f2(l, i):
m = deepcopy(l).remove(i)
return m
d = { i: f2(l,i) for l in t for i in l }
The first variant using, m = deepcopy(l); m.remove(i) works fine,
generating:
{'a': ['b', 'c', 'd', 'e', 'f'], 'c': ['a', 'b', 'd', 'e', 'f'], 'b':
['a', 'c', 'd', 'e', 'f'], 'e': ['a', 'b', 'c', 'd', 'f'], 'd': ['a',
'b', 'c', 'e', 'f'], 'g': ['h', 'i'], 'f': ['a', 'b', 'c', 'd', 'e'], 'i':
['g', 'h'], 'h': ['g', 'i'], 'm': ['l', 'n', 'o'], 'l': ['m', 'n', 'o'],
'o': ['l', 'm', 'n'], 'n': ['l', 'm', 'o']}
The second variant using, m = deepcopy(l).remove(i) fails thus:
{'a': None, 'c': None, 'b': None, 'e': None, 'd': None, 'g': None, 'f':
None, 'i': None, 'h': None, 'm': None, 'l': None, 'o': None, 'n': None}
I'm not sure I understand why after m = deepcopy(l); m.remove(i); m is a
different value to that which it as after m = deepcopy(l).remove(i).
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list