Posts: 39
Threads: 14
Joined: Oct 2018
Apr-26-2022, 08:39 PM
(This post was last modified: Apr-26-2022, 08:39 PM by leoahum.)
Hi,
I am trying to get a dictionary from a list like ["a", "b", "c", "a", "c"]. The out come is expected to be {"a": [0, 3], "b": [1], "c": [2, 4]}. The element of the list is the key and index of the element is the value. My script below doesn't work. Need some help with why it doesn't work and a solution. Thanks!
l = ["a","b","c","a","c"]
#create a dictionary with l's elements
d ={}.fromkeys(l, [])
#assign key and value by for loop
enum = enumerate(l)
for i,j in enum:
d[j].append(i)
print d
Posts: 1,583
Threads: 3
Joined: Mar 2020
Apr-26-2022, 08:58 PM
(This post was last modified: Apr-26-2022, 08:58 PM by bowlofred.)
Line4 is a problem. It assumes there is a single default value and assigns that to each. In your case that means each key is assigned the same empty list. When any key is updated, all the keys get updated because they're sharing the same list.
There are several ways to do this, but I might use setdefault(). I would append the position to the output of setdefault(). It will be the list if it already exists, or an empty list if it doesn't.
Your print line doesn't have parentheses. Are you running this under python2 or python3?
l = ["a","b","c","a","c"]
#create a dictionary
d = {}
#assign key and value by for loop
enum = enumerate(l)
for i,j in enum:
d.setdefault(j, []).append(i)
print (d) Output: {'a': [0, 3], 'b': [1], 'c': [2, 4]}
Posts: 39
Threads: 14
Joined: Oct 2018
Thanks a lot. It is super helpful! FYI, I'm using Ironpython 2.7 that embeded in some other software.
(Apr-26-2022, 08:58 PM)bowlofred Wrote: Line4 is a problem. It assumes there is a single default value and assigns that to each. In your case that means each key is assigned the same empty list. When any key is updated, all the keys get updated because they're sharing the same list.
There are several ways to do this, but I might use setdefault(). I would append the position to the output of setdefault(). It will be the list if it already exists, or an empty list if it doesn't.
Your print line doesn't have parentheses. Are you running this under python2 or python3?
l = ["a","b","c","a","c"]
#create a dictionary
d = {}
#assign key and value by for loop
enum = enumerate(l)
for i,j in enum:
d.setdefault(j, []).append(i)
print (d) Output: {'a': [0, 3], 'b': [1], 'c': [2, 4]}
Posts: 1,093
Threads: 143
Joined: Jul 2017
Apr-27-2022, 01:17 AM
(This post was last modified: Apr-27-2022, 01:23 AM by Pedroski55.)
I don't know anything about dict.setdefault() or where it might be useful.
You could achieve the same result like this:
mylist = ["a", "b", "c", "a", "c"]
# make a dictionary of empty lists
mydict = {l:[] for l in mylist}
# populate the lists
for v, k in enumerate(mylist):
mydict[k].append(v) How to put these 2 steps in a one-liner, I don't know!
Posts: 6,783
Threads: 20
Joined: Feb 2020
This is not easy to read:
l = ["a","b","c","a","c"]
d = {key:[index for index, value in enumerate(l) if value == key] for key in l}
print (d) Output: {'a': [0, 3], 'b': [1], 'c': [2, 4]}
Pedroski55 and leoahum like this post
Posts: 1,093
Threads: 143
Joined: Jul 2017
Very cool! Well done!
I would never have figured thatt out!
Posts: 39
Threads: 14
Joined: Oct 2018
Thank you all for the wonderful solutions!
Posts: 8,158
Threads: 160
Joined: Sep 2016
Just for sake of completeness, to mention collections.defaultdict
from collections import defaultdict
mylist = ["a", "b", "c", "a", "c"]
result = defaultdict(list)
for idx, item in enumerate(mylist):
result[item].append(idx)
print(result)
|