我有一个功能列表:
[(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')]
列表中的每个元素都是由要素和重要性值组成的元组。
我有一个列表字符串(后缀):“ SACC_MARKET_SEGMENT”,“ country_code_destination”,“ leading_bu”
我的目标是计算abaove引用的后缀列表中具有相同后缀的list元素的总和。
例如,在这里我会有这样的结果:
(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')
您能帮助解决此问题吗?
谢谢
答案 0 :(得分:0)
您是说这样吗?
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]
答案 1 :(得分:0)
这看起来并不漂亮,但是它将为您提供每个“根”的总和,即
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()
给出,
root SACC 0.021394 count 0.375210 country 0.031170 leading 0.010292 Name: value, dtype: float64
答案 2 :(得分:0)
这是使用sum
和filter
和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
答案 3 :(得分:0)
如果“根”始终相同,则可以使用正则表达式捕获它们。
使用此解决方案,它更改了原始列表。
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)