potatasbravas
potatasbravas

Reputation: 161

Loop through data looking for values from another list and add that to a dictionary

The output of a geochemical model I'm using generates 3000+ steps with 300ish chemical species per step.

I have a list of values with the species I'm interested in.

How can I loop through the data (main), using a list of the species I'm interested (species) in and then add the second value of the relevant species list to a key in a dictionary without having to type out 100 if statements if x=='y':list['x'].append(value) for each species?

This is a simplified version of my code:

main =[['a',1,2,3],['b',4,5,6],['c',7,8,9],['a',10,11,12],['b',13,14,15],['c',16,17,18]
species = ['a', 'b', 'c'] 
maindict={'a':[],'b':[],'c':[]}   

for value in main2:
    for x in value:
        if x=='a':maindict['a'].append(value[2])
        elif x=='b':maindict['b'].append(value[2])
        elif x=='c':maindict['c'].append(value[2])

What I'm looking for is something a bit simpler like:

for value in main:
    if value==i for i in species:
        maindict[i].append(value[2])

but obviously this doesn't really work.

Output:

maindict={'a':[3,12],'b':[6,15],'c':[9,18]

Upvotes: 2

Views: 53

Answers (2)

Kyle54
Kyle54

Reputation: 133

You can loop through each entry in main and check if the first entry is in maindict, then append the correct entry into the maindict

main =[['a',1,2,3],['b',4,5,6],['c',7,8,9],['a',10,11,12],['b',13,14,15],['c',16,17,18]]
species = ['a', 'b', 'c'] 
maindict={'a':[],'b':[],'c':[]}


for entry in main:
    if entry[0] in maindict:
        maindict[entry[0]].append(entry[3])
    else:
        maindict[entry[0]] = [entry[3]]

print maindict

>>> {'a': [3, 12], 'c': [9, 18], 'b': [6, 15]}

Upvotes: 1

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

The point of having a dict is to avoid sequential lookup isn't it ?

from collections import defaultdict

def collect(source, species):
    result = defaultdict(list)
    for key in species:
        found = source.get(key)
        if found:
            result[key].append(found[2])
    return result


source ={'a':[1,2,3],'b':[4,5,6],'c':[7,8,9], 'd':[0, 0, 0]}
species = ['a', 'b', 'c', 'e'] 

result = collect(source, species)
print result

EDIT : ok so your source is not a dict actually... Here's an updated version working on a list of lists (note that your source should probably be a list of tuples but that's another point):

from collections import defaultdict

def collect(source, species):
    result = defaultdict(list)
    for row in source:
        key = row[0]
        if key in species:
            result[key].append(row[2])
    return result


source =[['a', 1,2,3], ['b', 4,5,6],['c', 7,8,9], ['d', 0, 0, 0]}
# a set provides a huge perf improvement over a plain list here  
species = set(['a', 'b', 'c', 'e'])  

result = collect(source, species)
print result

Upvotes: 0

Related Questions