简体   繁体   English

使用python的列表中元素的总和

[英]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"我有一个列表字符串(后缀):“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.我的目标是计算在 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 .这是使用sumfilter以及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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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