需要基于源列表的元组索引来排列目标列表的元组

时间:2018-11-19 06:13:50

标签: python python-3.x list tuples

我有两个列表

Source = [('A','B','IP1','N1'),('K','G','IP2','N2'),('W','T','IP4','N4'),('K','L','IP3','N3') ]
Target = [('B','A','IP5','N5'), ('T','W','IP6','N6'), ('G','K','IP2','N2')]

这是我的代码:

source_tuples = set([s[:2][::-1] for s in Source])
target_tuples = set([t[:2] for t in Target])
missed_target_tuples = list(source_tuples - target_tuples)
Target.extend(missed_target_tuples)
Re_Target = Target

使用我的代码:在Source中,在Target的反向1个2个字符元组中找不到任何第1个2个字符的元组,然后按照相反的方式将它们添加到Target中,如Re_Target列表中所示

Re_Target = [('B', 'A', 'IP5', 'N5'), ('T', 'W', 'IP6', 'N6'), ('G', 'K', 'IP2', 'N2'), ('L', 'K')]

但是从源到目标的元组的索引不同,可以进行一些更改以获取此输出:

Re_Target = [('B', 'A', 'IP5', 'N5'),('G', 'K', 'IP2', 'N2'), ('T', 'W', 'IP6', 'N6'), ('L', 'K')]

0 个答案:

没有答案