无法检索两个列表之间的差异

时间:2017-07-28 09:50:45

标签: python list difference

设置向上

我有两个包含唯一ID' s tlyl的列表。

ID看起来像'97678410'

len(tl) = 185len(yl) = 182

问题

我需要两个列表之间的区别。也就是说,我想获取一个列表,其中包含ID但不在tl中的所有yl

我考虑过以下答案,

但是我的代码,

final = [id for id in tl if id not in yl]

产生final,这是tl的副本。

我已经检查过,IDtl中都有yltl = ['97682631', '97682633', '97684139', '97680023', '97680021', '97678410', '97675792', '97673883', '97673946', '97671417', '97664888', '97666272', '97667377', '97670240', '97664192', '97655130', '97655288', '97655289', '97655293', '97645033', '97645101', '97632355', '97633891', '97616084', '97608150', '97606844', '97602650', '97584169', '97595622', '97583025', '97577940', '97569579', '97570582', '97565137', '97540897', '97539337', '97518058', '97518763', '97512249', '97512252', '97493818', '97489271', '97489272', '97461011', '97459708', '97456572', '97456626', '97456632', '97453137', '97454483', '97454693', '97454892', '97450659', '97446162', '97446168', '97434415', '97436210', '97427637', '97427635', '97427632', '97425356', '97417309', '97404715', '97404716', '97404718', '97392000', '97392111', '97386620', '97383091', '97384274', '97384337', '97383090', '97383089', '97366670', '97361744', '97361698', '97361701', '97361702', '97361722', '97276849', '97272708', '97236813', '97223049', '97213666', '97174233', '97164586', '97141165', '97141170', '97145252', '97136643', '97130696', '97042669', '97036543', '97007304', '96915042', '96887981', '96883537', '96865568', '96818145', '96818147', '96776783', '96767917', '96747326', '96747763', '96735976', '96739100', '96712470', '96714575', '96695120', '96667787', '96561238', '96583133', '96558744', '96553823', '96528225', '96389164', '96377527', '96251693', '96269789', '96246548', '96171491', '96161689', '96153980', '96131831', '96131903', '96131956', '96131749', '96126777', '96101829', '95958417', '95914030', '95735741', '95656954', '95503961', '95501454', '95502625', '95474681', '95468325', '95472748', '95474209', '95440076', '95443633', '95408190', '95276367', '95222901', '95218637', '95218977', '95187313', '94948878', '94806104', '94306925', '94097967', '94018001', '94090391', '93860264', '93423007', '93206111', '92251067', '92480603', '91550754', '89421778', '89658137', '89123891', '88860045', '88903715', '88169920', '85395060', '83483635', '82819637', '81788095', '80286689', '76733816', '74983036', '75270743', '72079817', '69163539', '66080651', '62508733', '58272006', '58927395', '59487908', '59764478', '57909458', '51546448', '41278948'] yl = [97655130, 97645101, 97642334, 97642352, 97633891, 97636938, 97632355, 97616084, 97606844, 97608150, 97605869, 97602650, 97584169, 97595622, 97583025, 97577940, 97574727, 97569579, 97570582, 97566414, 97565137, 97540897, 97539337, 97518058, 97518763, 97512252, 97512249, 97493818, 97489271, 97489272, 97461011, 97459708, 97456572, 97456626, 97456632, 97453137, 97454483, 97454693, 97454892, 97446162, 97446168, 97450659, 97436210, 97434415, 97427632, 97427635, 97427637, 97425356, 97417309, 97404718, 97404715, 97404716, 97392000, 97392111, 97392693, 97386620, 97383091, 97384274, 97384337, 97383090, 97383089, 97366670, 97361698, 97361701, 97361702, 97361722, 97361744, 97362574, 97276849, 97272708, 97236813, 97223049, 97213666, 97205682, 97174233, 97164586, 97141165, 97141170, 97145252, 97136643, 97130696, 97129530, 97042669, 97036543, 97007304, 96915042, 96890889, 96890891, 96883537, 96865568, 96818147, 96818145, 96801213, 96776783, 96767917, 96747326, 96747763, 96735976, 96739100, 96712470, 96714575, 96704316, 96695120, 96583133, 96558744, 96561238, 96553823, 96528225, 96466854, 96389164, 96377527, 96306028, 96251693, 96269789, 96246548, 96171491, 96153980, 96161689, 96131956, 96126777, 96131749, 96131831, 96131903, 96101829, 95958417, 95914030, 95735741, 95656954, 95503961, 95501454, 95502625, 95474681, 95468325, 95472748, 95474209, 95440076, 95443633, 95408190, 95276367, 95222901, 95218637, 95218977, 95187313, 95192968, 94948878, 94806104, 94306925, 94170831, 94097967, 94018001, 94090391, 93423007, 93206111, 92480603, 92251067, 91550754, 89865094, 89421778, 89658137, 89123891, 88860045, 88903715, 88169920, 85395060, 83483635, 82819637, 81788095, 80260371, 80286689, 76733816, 74983036, 75270743, 72079817, 69163539, 66080651, 62508733, 59487908, 59764478, 58272006, 58927395, 51546448, 41278948]

请参阅两个列表here

final

我很确定这是一个愚蠢的事情,因为代码适用于一个简单的例子。

如何获取ID tlyl以内int my_sum(int a, int b) {return a + b;} int main() { int res; Test* tp = GetTestPointer(); res = my_sum(1, 2); int a = tp->value; int b = tp->value2; cout << a << endl; cout << b << endl; return 0; } 以外的所有Test

3 个答案:

答案 0 :(得分:2)

如何使用set数据结构:

>>> t1 = [2, 3, 4, 6]
>>> y1 = [2, 5, 7, 3]
>>> t1 = set(t1)
>>> y1 = set(y1)
>>> t1.difference(y1)
{4, 6}

答案 1 :(得分:1)

Processor
如果您确定没有重复值,则不需要

以上两行,否则也将比较重复值

tl = ['97682631', '97682633', '97684139', '97680023',........., '51546448', '41278948']
yl = [97655130, 97645101, 97642334, 97642352,.........., 51546448, 41278948]

tl_unique = set(tl) # to make sure your current list contain set of all unique values
yl_unique = set(yl)

答案 2 :(得分:1)

为清晰起见,使用两个略微简化的列表(一个带字符串,一个带整数):

tl = ['1', '2', '3']
yl = [1, 2, 4, 5]

tl中的所有字符串转换为整数,以便能够将它们与yl

中的整数相匹配
tl = [int(num) for num in tl]

使用set()工厂函数将两个列表转换为集合。

注意:正如您所提到的,这假设每个列表中的值都是唯一的。 set()会重复删除值,而不会发出警告。

tl = set(tl)
yl = set(yl)

使用内置集方法.difference()来识别第一组中不在第二组中的值。

final = tl.difference(yl)