Python comparison in a list of lists of tuples
我是Python的新手,我有一个关于元组列表比较的问题。我有一个列表,其中有元组中第二个元素的副本,我只想打印这个元组的第一个外观。例如,对于此列表:
1 | [(1, 2), (5, 10), (6, 10), (24, 35), (30, 35)] |
我有这个功能:
1 2 3 4 5 6 7 8 | def func(lst): list_first = [] #Checks if 2 elements have the same second value. for i in range(len(lst)-1): if(lst[i][1] == lst[i+1][1]): first = (lst[i][0]),lst[i][1] list_first.append(first) #append only the first element print list_first |
号
我的函数的输出是:
1 | [(5, 10), (24, 35), (30, 35)] |
但我的预期产出是:
1 | [(5, 10), (24, 35)] |
。
我怎么修?
更新
我必须在列表上做所有这些算法。所以对于输入:
1 | [[(0, 3), (1, 3), (2, 3), (3, 3), (4, 3)], [(5, 3), (6, 3), (7, 3), (8, 3), (9, 3)], [(10, 3), (11, 3), (12, 3), (13, 3), (14, 3)]] |
我希望输出为:
1 | [[(0, 3)], [(5, 3)], [(10, 3)]] |
。
我试过了,哦,改变我在这里得到的解决方案。但我得到的只是:
1 | [[(0, 3)], [(0, 3)], [(0, 3)]] |
。
不假设排序输入的解决方案
可以使用将元组中的第二个值用作键的字典。如果第二次出现此键,请将已经看到的元组追加到结果中。如果第三次或更多次看到元组,则不要再追加。集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from __future__ import print_function # makes work in Python 2 and 3 def find_first(lst): seen = {} res = [] added = set() for elem in lst: key = elem[1] if key in seen and key not in added: res.append(seen[key]) added.add(key) else: seen[key] = elem return res |
用LIS测试。注意最后一个元素
1 2 | L = [(1, 2), (5, 10), (6, 10), (24, 35), (30, 35), (60, 10)] print(find_first(L)) |
。
输出:
1 | [(5, 10), (24, 35)] |
它不使用排序,并且适用于此示例数据:
1 2 | L = [(1, 2), (6, 10), (5, 10), (24, 35), (30, 35), (60, 10)] print(find_first(L)) |
。
输出:
1 | [(6, 10), (24, 35)] |
。
另一个答案的解决方案不适用于此数据:
1 2 3 4 5 6 7 8 9 10 | from itertools import groupby L = [(1, 2), (6, 10), (5, 10), (24, 35), (30, 35), (60, 10)] final = [] for _, v in groupby(sorted(a) , lambda x : x[1]): b = list(v) if len(b) > 1: final.append(b[0]) print(final) |
输出:
1 | [(5, 10), (24, 35)] |
使用来自
编辑:就像@aks建议的那样,这里有一个具有多种输入形式的函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | from itertools import groupby def comp_list(a = list()): final = [] for _, v in groupby(sorted(a, key = lambda x : x[1]) , lambda x : x[1]): b = list(v) if len(b) > 1: final.append(b[0]) return final a1 = [(1, 2), (5, 10), (6, 10), (24, 35), (30, 35)] a2 = [(1, 2), (5, 10), (6, 10), (24, 35), (30, 35), (40, 35)] a3 = [(5, 10), (24, 35), (30, 35), (20, 5), (15, 4), (21, 5), (13, 4)] a4 = [(1, 2),(6, 10), (5, 10), (24, 35), (30, 35)] print(comp_list(a1)) print(comp_list(a2)) print(comp_list(a3)) print(comp_list(a4)) |
输出:
1 2 3 4 | [(5, 10), (24, 35)] [(5, 10), (24, 35)] [(15, 4), (20, 5), (24, 35)] [(6, 10), (24, 35)] |
号
这里有一个小方法可以帮助你
1 2 3 4 5 6 7 8 9 10 11 12 13 | def removeDuplicateTuple(sampleList): uniqueList = [] entryLog = {} processedEntries = [] for x, y in sampleList: if entryLog.get(y)==None: entryLog[y] = (x,y) else: if(entryLog.get(y) not in processedEntries): uniqueList.append(entryLog.get(y)) processedEntries.append(entryLog.get(y)) return uniqueList |
测试:埃多克斯1〔5〕
输出:埃多克斯1〔6〕