简体   繁体   中英

Sum of element in a list with python

I have a list of feature :

[(0.14409883378622257, 'count_90'), (0.1274820635854658, 'count_60'), (0.10362930186446877, 'count_30'), (0.017066033814948037, 'country_code_destination_ID'), (0.014104260612047153, 'country_code_destination_US'), (0.011414953372550486, 'SACC_MARKET_SEGMENT_BDZ'), (0.010291762087645236, 'leading_bu_PP'), (0.009979426898654558, 'SACC_MARKET_SEGMENT_IT3')]

Each element of the the list is a tuple composed from feature and a importance value.

I have a list strings (suffix) : "SACC_MARKET_SEGMENT", "country_code_destination", "leading_bu"

My objective is a count the sum of the element of list having the same suffix mentionned in the list of suffix cited abaove.

Here, for example i would have a result like this :

(0.14409883378622257, 'count_90')
(0.1274820635854658, 'count_60')
(0.10362930186446877, 'count_30')
(0.017066033814948037 + 0.014104260612047153 'country_code_destination)
(0.011414953372550486 + 0.009979426898654558 'SACC_MARKET_SEGMENT')
(0.010291762087645236, 'leading_bu')

Can you help to resolve this problem ?

thank you

Do you mean like this?

from collections import Counter
a = [(0.14409883378622257, 'count_90'), (0.1274820635854658, 'count_60'), (0.10362930186446877, 'count_30'), (0.017066033814948037, 'country_code_destination_ID'), (0.014104260612047153,
                                                                                                                                                                     'country_code_destination_US'), (0.011414953372550486, 'SACC_MARKET_SEGMENT_BDZ'), (0.010291762087645236, 'leading_bu_PP'), (0.009979426898654558, 'SACC_MARKET_SEGMENT_IT3')]
cnt = Counter()
for item in a:
    cnt[item[1]] += item[0]

This does not look pretty but It will get you the sums for each 'root', ie

d1 = []; d2 = []
for i in l1:
    v1 = i[1].split('_')[0]
    v2 = i[0]
    d1.append(v1)
    d2.append(v2)

d1
#['count', 'count', 'count', 'country', 'country', 'SACC', 'leading', 'SACC']
d2
#[0.14409883378622257, 0.1274820635854658, 0.10362930186446877, 0.017066033814948037, 0.014104260612047153, 0.011414953372550486, 0.010291762087645236, 0.009979426898654558]
df1 = pd.DataFrame({'root':d1, 'value':d2})
df1
#      root     value
#0    count  0.144099
#1    count  0.127482
#2    count  0.103629
#3  country  0.017066
#4  country  0.014104
#5     SACC  0.011415
#6  leading  0.010292
#7     SACC  0.009979

df1.groupby('root')['value'].sum()

which gives,

 root SACC 0.021394 count 0.375210 country 0.031170 leading 0.010292 Name: value, dtype: float64

Here is an implementation using sum and filter and lambda .

l = [(10, 'a'), (20, 'x'), (100, 'ab'), (200, 'ba')]
In[111]: l
Out[111]: [(10, 'a'), (20, 'x'), (100, 'ab'), (200, 'ba')]
In[112]: x = list(filter(lambda x:'a' in x[-1], l))
In[113]: x
Out[113]: [(10, 'a'), (100, 'ab'), (200, 'ba')]
In[114]: z = sum(p for p,q in x)
In[115]: z
Out[115]: 310

If the "roots" are always the same, you can catch them with regex.

With this solution, it changes the original list .

import re

l = [(0.14409883378622257, 'count_90'), 
     (0.1274820635854658, 'count_60'), 
     (0.10362930186446877, 'count_30'), 
     (0.017066033814948037, 'country_code_destination_ID'),
     (0.014104260612047153, 'country_code_destination_US'),     
     (0.011414953372550486, 'SACC_MARKET_SEGMENT_BDZ'),  
     (0.010291762087645236, 'leading_bu_PP'), 
     (0.009979426898654558, 'SACC_MARKET_SEGMENT_IT3')]

country_code_value = 0
sacc_market_value = 0

# Loop on the list with indexes and increment values when catch regex pattern.
for i, t in enumerate(l):
    if re.search(r"country_code_destination", t[1]):
        # remove the element of the list and keep value in variable      
        country_code_value += l.pop(i)[0]       
    elif re.search(r"SACC_MARKET_SEGMENT", t[1]):
        # remove the element of the list and keep value in variable
        sacc_market_value += l.pop(i)[0]

# Make tuples
country_code_destination = (country_code_value, "country_code_destination")
sacc_market_segment = (sacc_market_value, "SACC_MARKET_SEGMENT")

# And append to the original list
l.append(country_code_destination)
l.append(sacc_market_segment)

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