嗨!我有一个列表列表,当子列表的第一个元素相等时,我需要添加其中的第二个元素并打印结果。我已经考虑了很久,但我似乎无法弄清楚如何做到这一点。这是我的问题的一个例子:
num_list = [[1, 2], [3, 4], [1, 2], [3, 4], [3, 4]]
# 0th and 2nd sublists both have 1 as their first element.
# sum = 2 + 2. print out 4.
# all the remaining sublists have 3 as their first element.
# sum = 4 + 4 + 4. print out 12.
非常感谢!
PS:我知道这种映射最好用字典完成,但这只是我问题的简化版本。我的实际程序有超过2个值的子列表,我需要比较多于1个需要相等的值。
答案 0 :(得分:6)
您可以使用defaultdict
:
from collections import defaultdict
num_list = [[1, 2], [3, 4], [1, 2], [3, 4], [3, 4]]
d = defaultdict(int)
for item in num_list:
d[item[0]] += item[1]
结果是:
>>> d
defaultdict(<type 'int'>, {1: 4, 3: 12})
答案 1 :(得分:1)
您仍然可以使用dictonary执行此任务。使用元组作为键:
>>> d = {(1,1): (2,2), (3,3): (4,4)}
>>> d
{(1, 1): (2, 2), (3, 3): (4, 4)}
>>> d[(1,1)]
(2, 2)
您可能还想了解Counter课程。如果你的元素更复杂,我建议将它们包装在对象中,并实现__add__
方法来自定义它们的组合方式。
from collections import Counter
c = Counter()
c[(1,1)] = 10
c[(2,2)] = 10
c[(1,1)] += 1
c2 = Counter()
c2[(2,2)] = 4
c2[(2,3)] = 5
给出了:
>>> c
Counter({(1, 1): 11, (2, 2): 10})
>>> c + c2
Counter({(2, 2): 14, (1, 1): 11, (2, 3): 5})
请注意,您不能将列表用作键,因为列表是可变的,因此不可删除。你必须使用元组。
答案 2 :(得分:0)
使用标准dict()
:
num_list = [[1, 2], [3, 4], [1, 2], [3, 4], [3, 4]]
d = dict()
for e in num_list:
#get() checks if key exists, if not - returns 0
d[e[0]] = d.get(e[0], 0) + e[1]
print(d)
打印:
{1: 4, 3: 12}
答案 3 :(得分:0)
您似乎没有足够准确地描述您的问题 你的真正问题只能通过你对@Blender的问题和答案的评论来理解。对于这个问题,他很好的解决办法不能立即解决我理解的问题,但是差不多。
这是一种扩展以满足您需求的方法:
# some toy example data - I understand you want the first 2 sub_list
# to be "merged" because BOTH strings in pos 0 and 2 match
data = [['42x120x1800', 50, '50x90x800', 60],
['42x120x1800', 8, '50x90x800', 10],
['2x10x800', 5, '5x9x80', 6]]
from collections import defaultdict
# I'm using a lambda to initialize the items of the dict
# to a two-element list of zeros
d = defaultdict(lambda :[0, 0])
for sub_list in data:
key = (sub_list[0], sub_list[2])
d[key][0] += sub_list[1]
d[key][1] += sub_list[3]
for key in d:
print key, d[key]
# ('2x10x800', '5x9x80') [5, 6]
# ('42x120x1800', '50x90x800') [58, 70]
如果您想回到数据的初始表示:
new_data = [[key[0], val[0], key[1], val[1]] for key, val in d.iteritems()]
# [['2x10x800', 5, '5x9x80', 6], ['42x120x1800', 58, '50x90x800', 70]]