Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Adding up values and subvalues of a list from dictionary

I'm new to python and have been searching for a way to fix my problem for a week now. I'm hoping ill find hints or help here.

I have a first list

 C=['L', 'E', 'Q', 'N', 'LE', 'EQ', 'QN', 'NL', 'LEQ', 'EQN', 'QNL', 'NLE', 'LEQN']

which I managed to break down to its components to make list F (it seemed logical to me)

 F=['L', 'E', 'Q', 'N', 'L', 'E', 'E', 'Q', 'Q', 'N', 'N', 'L', 'L', 'E', 'Q', 'E', 'Q', 'N', 'Q', 'N', 'L', 'N', 'L', 'E', 'L', 'E', 'Q', 'N']

now what i need to do is add up the the weights for each adjacent components from a dictionary i have

data={'G':57,'A':71,'S':87,'P':97,'V':99,'T':101,'C':103,'I':113,'L':113,'N':114,'D':115,'K':128,'Q':128,'E':129,'M':131,'H':137,'F':147,'R':156,'Y':163,'W':186}

For example, here is what I need to get in a list:

(weight of L) ## (weight of E) ## (weight of Q) ## (weight of N) ## (weight of L + weight of E) and so on and so forth

I know i'm asking a lot and I came here as a last resort. Any help is immensely appreciated , thanks in advance.

like image 950
waelbiochem Avatar asked Jun 25 '26 00:06

waelbiochem


1 Answers

F is not helpful, it removes the information about which characters should be together which you need for the summation. Try a list comprehension:

output = [sum(data[char] for char in s) for s in C]

which gives

[113, 129, 128, 114, 242, 257, 242, 227, 370, 371, 355, 356, 484]
like image 163
jonrsharpe Avatar answered Jun 27 '26 15:06

jonrsharpe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!