简体   繁体   English

我有 2 个元组列表,如何打印元组的第二个元素之间的差异,同时让第一个元素保持不变?

[英]I have 2 lists of tuples, how can I print the difference between the second element of the tuples while leaving the first element the same?

Example, I have two lists:例如,我有两个列表:

[('Joe', 10), ('Tim', 14), ('Toby', 20)]

[('Joe', 8), ('Tim', 18), ('Toby', 12)]

I want it to print:我希望它打印:

[('Joe', 2), ('Tim', -4), ('Toby', 8)]

What is the best way to do this?做这个的最好方式是什么?

Consider using a collections.Counter , which features a "subtraction" version of dict.update .考虑使用collections.Counter ,它具有dict.update的“减法”版本。 For example:例如:

>>> L1 = [('Joe', 10), ('Tim', 14), ('Toby', 20)]
>>> L2 = [('Joe', 8), ('Tim', 18), ('Toby', 12)]
>>> from collections import Counter
>>> c1 = Counter(dict(L1))
>>> c1.subtract(Counter(dict(L2)))
>>> print(list(c1.items()))
[('Joe', 2), ('Tim', -4), ('Toby', 8)]

An approach is to zip the contents and then parse each zipped pair, doing the math and name extraction at that time.一种方法是zip内容,然后解析每个压缩对,在那时进行数学和名称提取。

zip will produce an iterable that will generate a pair of items, zippered together from each of the input items... If you were to look at these pairs, the would look something like this: zip将生成一个可迭代对象,该对象将生成一对项目,从每个输入项目拉链到一起......如果你要查看这些对,它们看起来像这样:

(('Joe', 10), ('Joe', 8))
(('Tim', 14), ('Tim', 18))
(('Toby', 20), ('Toby', 12))

With each item paired, it is fairly straightforward to then manipulate OR process each pair.配对每个项目后,操作或处理每个项目就相当简单了。

>>> l1 = [('Joe', 10), ('Tim', 14), ('Toby', 20)] 
>>> l2 = [('Joe', 8), ('Tim', 18), ('Toby', 12)]                                                                                                                                                                   
>>> new_list = []                                                                                                                                                                                                  

>>> for element1, element2 in zip(l1, l2): 
...     new_list.append((element1[0], element1[1] - element2[1])) 
...                                                                                                                                                                                                                
>>> new_list                                                                                                                                                                                                       
[('Joe', 2), ('Tim', -4), ('Toby', 8)]

I think you want to subtract the values if the names match.如果名称匹配,我认为您想减去这些值。 Try this:尝试这个:

first = [('Joe', 10), ('Tim', 14), ('Toby', 20)]
second = [('Joe', 8), ('Tim', 18), ('Toby', 12)]

access_dict = {}  # used for matching the names
for name, value in first:
    access_dict[name] = value  # add to matcher dict

for name, value in second:
    if name in access_dict:  # skip if corresponding name was not in first
        access_dict[name] = access_dict[name] - value  # subtract value

print(list(access_dict.items()))

Output:输出:

[('Joe', 2), ('Tim', -4), ('Toby', 8)]

Note: this does not preserve the order of the names注意:这不会保留名称的顺序

You can use a list comprehension:您可以使用列表理解:

l1=[('Joe', 10), ('Tim', 14), ('Toby', 20)]
l2=[('Joe', 8), ('Tim', 18), ('Toby', 12)]

>>> [(t1[0],t1[1]-t2[1]) for t1,t2 in zip(l1,l2)]
[('Joe', 2), ('Tim', -4), ('Toby', 8)]

You can add a test to make sure that the first part of the tuple is the same if appropriate:如果合适,您可以添加测试以确保元组的第一部分相同:

[(t1[0],t1[1]-t2[1]) for t1,t2 in zip(l1,l2) if t1[0]==t2[0]]

But wim's version using a counter is the most robust if you have lists that might have mismatched tuples.但是,如果您的列表可能包含不匹配的元组,那么使用计数器的wim版本是最健壮的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定一个元组列表,如果第一个元素相同,我如何在每个相似的元组中添加第二个元素 - Given a list of tuples, if the first element is the same, how do i add the second element inside each similar tuple 如果元组具有相同的第一个元素,如何聚合元组列表的元素? - how to aggregate elements of a list of tuples if the tuples have the same first element? 在python中如何通过元组元素匹配两个元组列表? - In python how can I match two lists of tuples by a tuple element? Python 元组:我怎样才能只有一个元素作为对? - Python tuples: How can i have only one element as pair? 如何在 2 元组集中找到具有特定第一个元素的 2 元组? - How can I find 2-tuples with a specific first element in the set of 2-tuples? 元组列表列表,按第一个元素分组并添加第二个元素 - List of lists of tuples, group by first element and add second elements Python 3.x:如何按字符串值对具有相同第二个元素(计数)的元组列表进行排序? - Python 3.x: How do I sort a list of tuples that has the same second element (count) by string value? 如何获取具有匹配的第二个元素的元组的第一个元素(python) - how to get first element of tuples with matching second element (python) 根据字典中列表中元组的第二个元素排序 - Sorting according to Second Element of Tuples in Lists in a Dictionary 从 Python 中的元组列表列表中获取元组的所有第一个元素 - Get all first element of tuples from list of lists of tuples in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM