我有一个很大的元组列表,其中每个元组包含9个字符串元素:
pdf_results = [
("Kohl's - Dallas", '-', "Kohl's Cafe", '03/18/22', 'RC', '8', '0', '16', '8'),
("Kohl's - Dallas", '-', "Kohl's Cafe", '03/18/22', 'SMI', '5', '0', '10', '5'),
("Kohl's - Dallas", '-', "Kohl's Cafe", '03/19/22', 'RC', '8', '0', '16', '8'),
("Kohl's - Dallas", '-', "Kohl's Cafe", '03/19/22', 'SMI', '5', '0', '10', '5'),
("Kohl's - Dallas", '-', "Kohl's Cafe", '03/20/22', 'RC', '8', '0', '16', '8'),
("Kohl's - Dallas", '-', "Kohl's Cafe", '03/20/22', 'SMI', '5', '0', '10', '5'),
("Kohl's - Dallas", '-', "Kohl's Cafe", '03/21/22', 'RC', '8', '0', '16', '8'),
("Kohl's - Dallas", '-', "Kohl's Cafe", '03/21/22', 'SMI', '5', '0', '10', '5'),
("Kohl's - Dallas", '-', "Kohl's Cafe", '03/23/22', 'SMI', '5', '0', '10', '5'),
("Kohl's - Dallas", '-', "Kohl's Cafe", '03/24/22', 'RC', '8', '0', '16', '8'),
("Kohl's - Dallas", '-', "Kohl's Cafe", '03/24/22', 'SMI', '5', '0', '10', '5'),
('Bronx-Lebanon Hospital Center', '-', 'Patient Trayline ', '03/18/22', 'RC', '8', '0', '16', '8'),
('Bronx-Lebanon Hospital Center', '-', 'Patient Trayline ', '03/18/22', 'SMI', '5', '0', '10', '5'),
('Bronx-Lebanon Hospital Center', '-', 'Patient Trayline ', '03/19/22', 'RC', '8', '0', '16', '8'),
('Bronx-Lebanon Hospital Center', '-', 'Patient Trayline ', '03/19/22', 'SMI', '5', '0', '10', '5')
]
在不使用Pandas数据帧的情况下,如何最好地按每个元组的第一个元素分组,以便对每个元组的最后一个元素求和。输出应该是这样的:
desired_output = [
("Kohl's - Dallas", 70),
("Bronx-Lebanon Hospital Center", 26)
]
我尝试过使用itertools.groupby
,这似乎是最合适的解决方案;然而,在正确迭代、索引和求和每个元组的最后一个元素时,不会遇到以下障碍之一:
- 每个元组的最后一个元素的类型为
string
,并且在转换为int
时防止作为TypeError: 'int' object not iterable
进行迭代 ValueError
被提升,其中invalid literal for int() with base 10: 'b'
尝试:
from itertools import groupby
def getSiteName(siteChunk):
return siteChunk[0]
siteNameGroup = groupby(pdf_results, getSiteName)
for key, group in siteNameGroup:
print(key) # 1st element of tuple as desired
for pdf_results in group:
# Raises TypeError: unsupported operand type(s) for +: 'int' and 'str'
print(sum(pdf_results[8]))
print()
假设您的列表按第一个元素排序,您可以执行:
from itertools import groupby
for k,v in groupby(pdf_results, key=lambda t: t[0]):
print(k, sum(int(x[-1]) for x in v))
打印:
Kohl's - Dallas 70
Bronx-Lebanon Hospital Center 26
如果顺序没有排序,只需使用dict
来合计元组的第一个条目键入的元素:
res={}
for t in pdf_results:
res[t[0]]=res.get(t[0],0)+int(t[-1])
>>> res
{"Kohl's - Dallas": 70, 'Bronx-Lebanon Hospital Center': 26}
为什么不在空字典上使用简单的for
循环?
resultDict = {}
for value in pdf_results:
if value[0] not in resultDict:
resultDict[value[0]] = 0
resultDict[value[0]] += float(value[len(value)-1])
print(resultDict)
输出
{"Kohl's - Dallas": 70.0,
'Bronx-Lebanon Hospital Center': 26.0}
如果字典不是你想要的,并且你坚持要有一个元组,你可以使用:
list(resultDict.items())
输出
[("Kohl's - Dallas", 70.0), ('Bronx-Lebanon Hospital Center', 26.0)]
您就快到了。只需更改
for pdf_results in group:
print(sum(pdf_results[8]))
至:
print(sum(int(pdf_results[8])
for pdf_results in group))
(尽管我也会重命名为pdf_result
,单数。(
这也可以:
from collections import defaultdict
output = defaultdict(int)
for item in pdf_results:
output[item[0]] += int(item[-1])
print(list(output.items()))
输出
[("Kohl's - Dallas", 70), ('Bronx-Lebanon Hospital Center', 26)]