Python Forum
How to make this simpler. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to make this simpler. (/thread-16398.html)



How to make this simpler. - leoahum - Feb-26-2019

D = ['a','b','c','d','e']

l = [[233,'a'],[321,'a'],[445,'b'],[322,'c'],[123,'c'],[12312,'c'],[456,'d'],[334,'e']]


ND = {}

for x in l:
    if x[1] in D:
        ND[x[1]] = []
for x in l:
    ND[x[1]].append(x[0])


print(ND)
I'm trying to make a dictionary based on the two lists above. "D" is the list for keys. "l" is a "data pool". I got what I want, but I'm still looking for a better solution.


RE: How to make this simpler. - ichabod801 - Feb-26-2019

One thing you can do is just delete line 11. Leave what is now line 12 indented and it will still work.

Another solution is collections.defaultdict:

import collections

l = [[233,'a'],[321,'a'],[445,'b'],[322,'c'],[123,'c'],[12312,'c'],[456,'d'],[334,'e']]
ND = collections.defaultdict(list)

for x in l:
    ND[x[1]].append(x[0])



RE: How to make this simpler. - leoahum - Feb-26-2019

(Feb-26-2019, 04:45 PM)ichabod801 Wrote: One thing you can do is just delete line 11. Leave what is now line 12 indented and it will still work.

Another solution is collections.defaultdict:

import collections

l = [[233,'a'],[321,'a'],[445,'b'],[322,'c'],[123,'c'],[12312,'c'],[456,'d'],[334,'e']]
ND = collections.defaultdict(list)

for x in l:
    ND[x[1]].append(x[0])

Thank you for the solution. But I'm using IronPython, trying to import as less as I can.

D = ['a', 'b', 'c', 'd', 'e']
l = [[233, 'a'], [321, 'a'], [445, 'b'], [322, 'c'], [123, 'c'], [12312, 'c'], [456, 'd'], [334, 'e']]

ND = dict.fromkeys(D,[])

print (ND)


for x in l:
    ND[x[1]].append(x[0])

print(ND)
I tried using dict.fromkeys, but it give me a different result. It will put all x[0] under each key of the dictionary. Comparing with the script below, their first "print" give same kind of dictionary. Their second steps are the same. Could anyone tell me why the results are different?

D = ['a', 'b', 'c', 'd', 'e']
l = [[233, 'a'], [321, 'a'], [445, 'b'], [322, 'c'], [123, 'c'], [12312, 'c'], [456, 'd'], [334, 'e']]

ND = {}

for x in l:
    if x[1] in D:
        ND[x[1]] = []

print (ND)

for x in l:
    ND[x[1]].append(x[0])

print(ND)



RE: How to make this simpler. - ichabod801 - Feb-26-2019

Dictionaries and lists are mutable objects, so when you reassign them, you reassign a link (or pointer) to them, to the value they hold. So your dict.fromkeys is assigning the same empty list to each dictionary key. To make a dictionary of independent empty lists you can use a dictionary comprehension:

ND = {key: [] for key in D}



RE: How to make this simpler. - ichabod801 - Feb-26-2019

(Feb-26-2019, 05:52 PM)leoahum Wrote: I'm using IronPython, trying to import as less as I can.

I looks like Iron Python does have the collections library, which is part of the standard library for C Python: https://ironpython-test.readthedocs.io/en/latest/library/collections.html


RE: How to make this simpler. - leoahum - Feb-26-2019

(Feb-26-2019, 06:35 PM)ichabod801 Wrote:
(Feb-26-2019, 05:52 PM)leoahum Wrote: I'm using IronPython, trying to import as less as I can.

I looks like Iron Python does have the collections library, which is part of the standard library for C Python: https://ironpython-test.readthedocs.io/en/latest/library/collections.html

Thanks a lot for your help!


RE: How to make this simpler. - perfringo - Mar-06-2019

One way to achieve desired result without import:

>>> D = ['a','b','c','d','e']
>>> l = [[233,'a'],[321,'a'],[445,'b'],[322,'c'],[123,'c'],[12312,'c'],[456,'d'],[334,'e']]
>>> d = dict()
>>> for v, k in l:
...     if k in D:
...         d.setdefault(k, []).append(v)
...
>>> d
{'a': [233, 321], 'b': [445], 'c': [322, 123, 12312], 'd': [456], 'e': [334]}



RE: How to make this simpler. - leoahum - Mar-11-2019

(Mar-06-2019, 11:23 AM)perfringo Wrote: One way to achieve desired result without import:

>>> D = ['a','b','c','d','e']
>>> l = [[233,'a'],[321,'a'],[445,'b'],[322,'c'],[123,'c'],[12312,'c'],[456,'d'],[334,'e']]
>>> d = dict()
>>> for v, k in l:
...     if k in D:
...         d.setdefault(k, []).append(v)
...
>>> d
{'a': [233, 321], 'b': [445], 'c': [322, 123, 12312], 'd': [456], 'e': [334]}

Thanks!