Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make this simpler.
#1
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.
Reply
#2
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])
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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)
Reply
#4
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}
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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/e...tions.html
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(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/e...tions.html

Thanks a lot for your help!
Reply
#7
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]}
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.
Reply
#8
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can you make it simpler? bitcoin10mil 5 2,424 Aug-22-2020, 08:38 PM
Last Post: snippsat
  i am looking for a simpler tokenizer Skaperen 7 3,448 Jul-29-2019, 05:31 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020