As a beginner using Python, I'm trying to populate a dictionary with values using an if statement. If you have ideas as to why this isn't working, I would appreciate your input. Mahalo in advance!

#first list
groslist = [0, 1, 2, 3]
#dictionary whose keys are items in grosList
grosdict = {k:[] for k in groslist}
#second list whose items correspond with dictionary's keys
indivlist = [0, 0, 1, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3]
#third list whose nth item ID corresponds indivlist
indivIDlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

#Populate dictionary with indivIDlist values (dict keys correspond with indivlist items)
for ID in indivlist:
    for ID in groslist:
        x = 0;
        if indivlist[x] == groslist[x]:
            grosdict[indivlist[x]] = indivIDlist[x];
        x += 1

Here is my ideal output:

print CG_IDdict
{0: [0, 1, 3], 1: [2, 4, 5, 6], 2: [7, 8, 9, 10], 3: [11, 12]}
有帮助吗?

解决方案

Looking at the expected output, this seems to be what you're trying to do.

for key, value in zip(indivlist, indivIDlist):
    grosdict[key].append(value)
print grosdict
#{0: [0, 1, 3], 1: [2, 4, 5, 6], 2: [7, 8, 9, 10], 3: [11, 12]}

the zip function. groups up the elements of the two list's together like this:

[(0, 0), (0, 1), (1, 2), (0, 3), (1, 4), (1, 5), (1, 6), (2, 7), (2, 8), (2, 9), (2, 10), (3, 11), (3, 12)]

and then looping trough this list we have the value, and the corresponding key, to insert them into the dictionary.

Taking a look at the original code. There are some problems there.

  • Same variable name in both the two nested loop.
    • Altough you're not using these variables, you probably should
  • You're resetting the variable x to zero at the start of every loop.
  • You're trying to index different lists, of different length, with the same variable.
  • You're assigning (replacing) the matching values, instead of appending them.

This modified code gives the same result.

x = 0
for ID_indiv in indivlist:
    for ID_grod in groslist:
        if ID_indiv == ID_grod:
            grosdict[ID_indiv].append(indivIDlist[x])
    x += 1

or you could do a small edit and use enumerate:

for x, ID_indiv in enumerate(indivlist):
    for ID_grod in groslist:
        if ID_indiv == ID_grod:
            grosdict[ID_indiv].append(indivIDlist[x])

其他提示

import collections

groslist = [0, 1, 2, 3]

grosdict = collections.defaultdict(list) # Uses the standard library’s default dictionary type.

indivlist = [0, 0, 1, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3]
indivIDlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

for key, value in zip(indivlist, indivIDlist):
    grosdict[key].append(value)

assert grosdict == {0: [0, 1, 3],
                    1: [2, 4, 5, 6],
                    2: [7, 8, 9, 10],
                    3: [11, 12]}

print(dict(grosdict))

Using the built-in zip function like M4rtini stated is probably the most elegant solution. Using the standard library's defaultdict type, in lieu of a dictionary comprehension may also help.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top