在Python中,计算两个列表之间差异的最佳方法是什么?
例
A = [1,2,3,4]
B = [2,5]
A - B = [1,3,4]
B - A = [5]
Run Code Online (Sandbox Code Playgroud) 例如,我有两个列表
A = [6, 7, 8, 9, 10, 11, 12]
subset_of_A = [6, 9, 12]; # the subset of A
the result should be [7, 8, 10, 11]; the remaining elements
Run Code Online (Sandbox Code Playgroud)
在python中是否有内置函数来执行此操作?
继承我的代码:
item = [0,1,2,3,4,5,6,7,8,9]
z = [] # list of integers
for item in z:
if item not in z:
print item
Run Code Online (Sandbox Code Playgroud)
Z包含整数列表.我想将项目与Z进行比较,并打印出与项目相比时不在Z中的数字.我可以打印Z中的elemtens,当比较不是项目时,但当我尝试使用上面的代码执行相反的操作时,没有打印.
有帮助吗?
比较2个列表/集合并输出差异的最简单方法是什么?是否有任何内置函数可以帮助我比较嵌套列表/集合?
输入:
First_list = [['Test.doc', '1a1a1a', 1111],
['Test2.doc', '2b2b2b', 2222],
['Test3.doc', '3c3c3c', 3333]
]
Secnd_list = [['Test.doc', '1a1a1a', 1111],
['Test2.doc', '2b2b2b', 2222],
['Test3.doc', '8p8p8p', 9999],
['Test4.doc', '4d4d4d', 4444]]
Run Code Online (Sandbox Code Playgroud)
预期产出:
Differences = [['Test3.doc', '3c3c3c', 3333],
['Test3.doc', '8p8p8p', 9999],
['Test4.doc', '4d4d4d', 4444]]
Run Code Online (Sandbox Code Playgroud) 我是编程新手,但我一直在学习,最近我碰到了墙,所以我正在寻求帮助.对不起,如果以前讨论过,但我找不到问题的答案.我有两个清单.我需要比较它们,并在结果中得到不匹配的对象.例如:
a = [1,2,3,4,5,6]
b = [1,2,3,4,5,6,7,8,9]
result = [7,8,9].
Run Code Online (Sandbox Code Playgroud)
我似乎只找到返回匹配的代码和示例.我不需要.
列表在文件记事本file.txt中供大家记住,如果这有助于你帮助我.:)
list1 = [{'key1': 'item1'}, {'key2': 'item2'}]
list2 = [{'key1': 'item1'}, {'key2': 'item2'}, {'key3': 'item3'}]
Run Code Online (Sandbox Code Playgroud)
有没有办法区分这两个列表?
基本上,我需要一种可扩展的方法来获取包含字典的2个列表之间的差异.所以我试图比较这些列表,然后得到回报{'key3': 'item3'}
说,我想计算两个列表的区别C = A - B:
A = [1,2,3,4,5,6,7,8,9]
B = [1,3,5,8,9]
C = [2,4,6,7] #Result
Run Code Online (Sandbox Code Playgroud)
A并且B都使用唯一的整数排序(不确定是否有办法告诉Python有关列表的此属性).我需要保留元素的顺序.AFAIK有两种可行的方法
方法1:将B转换为集合并使用列表解析来生成C:
s = set(B)
C = [x for x in A if x not in s]
Run Code Online (Sandbox Code Playgroud)
方法2:直接使用列表理解:
C = [x for x in A if x not in B]
Run Code Online (Sandbox Code Playgroud)
为什么#1效率更高#2?是否有转换为集合的开销?我在这里错过了什么?
更新:我知道一个集合的平均O(1)查找时间比一个列表的查询时间快,O(n)但如果原始列表A包含大约一百万左右的整数,那么集合创建实际上不会花费更长时间吗?
我想在 Python 中执行以下操作:
A = [1, 2, 3, 4, 5, 6, 7, 7, 7]
C = A - [3, 4] # Should be [1, 2, 5, 6, 7, 7, 7]
C = A - [4, 3] # Should not be removing anything, because sequence 4, 3 is not found
Run Code Online (Sandbox Code Playgroud)
所以,我只想从另一个列表中删除子列表(作为序列)的第一次出现。我怎样才能做到这一点?
编辑:我说的是列表,而不是集合。这意味着项目是排序(序列)物质(既在A和B),以及重复。
H,我正在尝试在 Python 中对两个字符串(应该先变成十六进制)进行异或。我知道一种方法会奏效:
def xor_two_str(str1, str2):
return hex(int(str1,16) ^ int(str2,16))
Run Code Online (Sandbox Code Playgroud)
但我试过这样的:
def change_to_be_hex(str):
return hex(int(str,base=16))
def xor_two_str(str1,str2):
a = change_to_be_hex(str1)
b = change_to_be_hex(str2)
return hex(a ^ b)
print xor_two_str("12ef","abcd")
Run Code Online (Sandbox Code Playgroud)
这将返回 TypeError: ^ 不应在 str, str 之间使用。我不知道为什么。
而且这个功能也不起作用:
bcd = change_to_be_hex("12ef")
def increment_hex(hex_n):
return hex_n + 1
result = increment_hex(bcd)
print result
Run Code Online (Sandbox Code Playgroud)
错误信息是:TypeError: cannot concatenate 'str' and 'int' objects 我觉得这很奇怪:(
谢谢!
我有3个列表,我想找到第1 /第2和第2 /第3之间的差异并打印它们.
这是我的代码:
n1 = [1,1,1,2,3]
n2 = [1,1,1,2] # Here the 3 is not found ("3" is not present in n1 at all)
n3 = [1,1,2] # here 1 is not found (there are fewer "1"s in n3 than in n2)
for x in n1:
if x not in n2:
print x
for m in n2:
if m not in n3:
print m
Run Code Online (Sandbox Code Playgroud)
但我得到的输出只有3.
如何使它输出1和3?我也尝试使用sets,但它只是3再次打印.
python ×10
list ×8
python-2.7 ×3
set ×2
compare ×1
filter ×1
match ×1
performance ×1
tuples ×1