Py学习  »  Python

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

Nasri • 4 年前 • 531 次点击  

我有一个功能列表:

[(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 U市场细分”、“国家代码目的地”、“主要目的地”

我的目标是计算在above所引用的后缀列表中具有相同后缀的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')

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

谢谢您

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43448
 
531 次点击  
文章 [ 4 ]  |  最新文章 4 年前
MrCed
Reply   •   1 楼
MrCed    5 年前

如果“根”总是相同的,你可以用正则表达式来捕获它们。

有了这个解决方案 更改原始列表 .

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)
Rohit
Reply   •   2 楼
Rohit    5 年前

下面是一个使用 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
Sotos
Reply   •   3 楼
Sotos    5 年前

这看起来不漂亮,但它会得到每个“根”的和,即。

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
Rouzip
Reply   •   4 楼
Rouzip    5 年前

你是说这样吗?

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]