0

What is the easiest way to do this without using defaultdict? If too cumbersome to do this with core python functionality, then defaultdict is okay.

I have a list of keys as a list.

I have a short loop to create a few lists with values, of which I want to map to the keys in the dict.

So, in the end I will get multiple values per key. Then print.

This is what I have so far...

Keys = ['A','B','C'] #the keys that will be mapped to all values start here in a list               
Values = [] #starting with empty values
Dict = dict(zip(Keys, Values)) #starting with a dict of keys mapped to empty values

x=0
while x < 5:
    x += 1 
    MoreValues = [1+x,2+x,3] #adding x only to give some different values
    Dict = dict(zip(Keys, MoreValues))

    print Dict

Output with this code is:

{'A': 2, 'C': 3, 'B': 3}
{'A': 3, 'C': 3, 'B': 4}
{'A': 4, 'C': 3, 'B': 5}
{'A': 5, 'C': 3, 'B': 6}
{'A': 6, 'C': 3, 'B': 7}   

Another way to look at the question, is how can these resulting dictionaries be merged together so the keys of A, B, C are mapped to those multiple values, given this setup with a for loop to generate the list of values.

Output should be:

{'A': [2, 3, 4, 5, 6], 'C': [3, 3, 3, 3, 3], 'B': [3, 4, 5, 6, 7]}
Steve
  • 331
  • 1
  • 6
  • 14
  • Why do you not want to use a defaultdict? – karthikr Sep 21 '14 at 22:07
  • I wanted to see how to do it through core python functionality, but if it is too cumbersome then defaultdict is okay. – Steve Sep 21 '14 at 22:10
  • Here are a few ways: http://stackoverflow.com/questions/5946236/how-to-merge-multiple-dicts-with-same-key – karthikr Sep 21 '14 at 22:13
  • It looks like in that example, there are multiple dictionaries with the same key. In this example it is multiple lists of values to map to a single dictionary with keys. – Steve Sep 21 '14 at 22:18
  • 1
    What output do you want at the end of the loop? I'm having a hard time understanding what it is you actually want here . . . – mgilson Sep 21 '14 at 22:18
  • Added this to problem statement. – Steve Sep 21 '14 at 22:23

3 Answers3

1

You could also use defaultdict

from collections import defaultdict
d = defaultdict(lambda: [])

for x in range(5):
    for key in keys:
        d[key].append(x)
BWStearns
  • 2,567
  • 2
  • 19
  • 33
0
keys = "ABC"
answer = {}

x = 1
while x<5:
    for key in keys:
        if key not in answer:
            answer[key] = []
        answer[key].append(x)
    x += 1

Of course, you could move the initialization out of the while loop:

keys = "ABC"
answer = {}
for key in keys:
    if key not in answer:
        answer[key] = []

x = 1
while x<5:
    for key in keys:
        answer[key].append(x)
    x += 1
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
0

you can try this code : so i write all details if you cant relies some part pls ask :

>>> d1={'A': 2, 'C': 3, 'B': 3l}
>>> d2={'A': 3, 'C': 3, 'B': 4}
>>> d3={'A': 4, 'C': 3, 'B': 5}
>>> d4={'A': 5, 'C': 3, 'B': 6}
>>> d5={'A': 6, 'C': 3, 'B': 7}
>>> keys=[d1.keys(),d2.keys(),d3.keys(),d4.keys(),d5.keys()]
>>> values=[d1.values(),d2.values(),d3.values(),d4.values(),d5.values()]
>>> keys
[['A', 'C', 'B'], ['A', 'C', 'B'], ['A', 'C', 'B'], ['A', 'C', 'B'], ['A', 'C', 'B']]
>>> values
[[2, 3, 3], [3, 3, 4], [4, 3, 5], [5, 3, 6], [6, 3, 7]]
>>> set_keys=[set(list(i)).pop() for i in zip(*keys)]
>>> set_keys
['A', 'C', 'B']
>>> set_values=zip(*values)
>>> set_values
[(2, 3, 4, 5, 6), (3, 3, 3, 3, 3), (3, 4, 5, 6, 7)]
>>> set_values=map(list,zip(*values))
>>> set_values
[[2, 3, 4, 5, 6], [3, 3, 3, 3, 3], [3, 4, 5, 6, 7]]
>>> dict(zip(set_keys,set_values))
{'A': [2, 3, 4, 5, 6], 'C': [3, 3, 3, 3, 3], 'B': [3, 4, 5, 6, 7]}
>>> 
Mazdak
  • 105,000
  • 18
  • 159
  • 188