列表中元素的总和与python

问题描述 投票:-1回答:4

我有一个功能列表:

[(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”

我的目标是计算列表元素的总和,该列表元素具有在引用的后缀列表中提到的相同后缀。

在这里,例如我会有这样的结果:

(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')

你能帮忙解决这个问题吗?

谢谢

python list
4个回答
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]

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

0
投票

这是使用sumfilterlambda的实现。

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

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)
© www.soinside.com 2019 - 2024. All rights reserved.