简体   繁体   中英

Iterating through list of list of tuples and a dictionary

I have a list of two lists, each with five tuples that have two elements. Like so:

cos = [ [('B6409', 0.9997), ('S4193', 0.9996), ('C9826', 0.9995), ('J6706', 0.9994), ('Q0781', 0.9993)], [('A5474', 0.9985), ('H1286', 0.9981), ('Y1178', 0.998), ('D2742', 0.9979), ('A7668', 0.9979)] ]

And a dictionary 'dist' like so:

dist = {'R7033': [93.9636, 32.6327, 33.092, 32.1305, 31.7842] , 'P1283': [100.081, 32.3075, 34.4401, 37.3563, 43.815] , 'W7831': [93.3658, 33.5885, 31.215, 30.8853, 33.0429] , 'B6409': [91.0789, 30.327, 30.4114, 32.973, 31.5477, 'J6706': [99.9582, 34.7457, 32.913, 37.0979, 34.8625] }

And what I'd like to do is that if a key from the dictionary is the same as one of the first elements from the tuple, sum up all the values of the same index for each value of that key. for example:

'B6409' matches with one of the dictionary keys 'B6409' and so does 'J6706'. So then, the first element of the list from the key 'B6409' must be added to the first element of the list from the key 'J6706'. And so forth with the second, third, 4th and 5th elements. Such that I would get:

(1st element from 'B6049' plus first element of 'J6706'): 91.0789 + 99.9582 = 191.0371

(2nd element from 'B6049' plus 2nd element of 'J6706'): 30.327 + 34.7457 = 65.0727

and so forth for each of the 5 elements from the keys that have a match...

So far Ive done some, (hard-coding i guess), however I think its not the best iteration, and I got an error;
TypeError: unsupported operand type(s) for +=: 'int' and 'str' @ ocw2 += i[0] (the starred line)

for i in dist:
        if i == cos[0][0][0]:
            ocw += i[0]
            refl += i[1]
            lefl += i[2]
            icw += i[3]
            nw += i[4]
           
    
        if i == cos[0][1][0]:
            ocw += i[0]
            refl += i[1]
            lefl += i[2]
            icw += i[3]
            nw += i[4]
            
            
        if i == cos[0][2][0]:
            ocw += i[0] ***********
            refl += i[1]
            lefl += i[2]
            icw += i[3]
            nw += i[4]
           

Is there a better way to iterate over the matched keys to find the sum of each index of the values of the dictionary?

You should try to format your lists and dictionaries like shown below. They are more readable this way and code is read more often than it is written.

cos = [
    [
        ('B6409', 0.9997),
        ('S4193', 0.9996),
        ('C9826', 0.9995),
        ('J6706', 0.9994),
        ('Q0781', 0.9993)
    ], 
    [
        ('A5474', 0.9985),
        ('H1286', 0.9981),
        ('Y1178', 0.998),
        ('D2742', 0.9979),
        ('A7668', 0.9979)
    ] 
]

dist = {
    'R7033': [93.9636, 32.6327, 33.092, 32.1305, 31.7842], 
    'P1283': [100.081, 32.3075, 34.4401, 37.3563, 43.815], 
    'W7831': [93.3658, 33.5885, 31.215, 30.8853, 33.0429], 
    'B6409': [91.0789, 30.327, 30.4114, 32.973, 31.5477], 
    'J6706': [99.9582, 34.7457, 32.913, 37.0979, 34.8625]
}

Whenever you see yourself doing a same thing over and over again, ask yourself if writing a function would be a better idea or not. Most of the times, the answer will be yes. It seems to me that you need to write a function here to improve (at-least) your code readability.

Your question isn't very clear but from what I've gathered about your issues, here's my answer to that-

# defining these as I don't have them in my namespace yet
# you should not paste this line
ocw = refl = lefl = icw = nw = 0


# Start copying from here
def some_func(l):
    global ocw, refl, lefl, icw, nw
    ocw += l[0]
    refl += l[1]
    lefl += l[2]
    icw += l[3]
    nw += l[4]


for k in dist:
    for sublist in cos:
        for t in sublist:
            if k == t[0]:
                some_func(dist[k])


print(ocw, refl, lefl, icw, nw)

Although the complexity will definitely be an issue for moderate to large dataset, this should do what you're attempting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM