Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3.x是否有数组_diff(php)方法模拟_Python_Arrays - Fatal编程技术网

Python3.x是否有数组_diff(php)方法模拟

Python3.x是否有数组_diff(php)方法模拟,python,arrays,Python,Arrays,例如: >> a = list(["red", "blue", "red"]); >> b = list(["yellow", "red"]); 数组_diff的结果: >> list("blue") 或者是最好的方法?将它们转换为一个集合并进行集合减法 >>> a = ["red", "blue", "red"] >>> b = ["yellow", "red"] >>> set(a) - set(b

例如:

>> a = list(["red", "blue", "red"]);
>> b = list(["yellow", "red"]);
数组_diff的结果:

>> list("blue")

或者是最好的方法?

将它们转换为一个集合并进行集合减法

>>> a = ["red", "blue", "red"]
>>> b = ["yellow", "red"]
>>> set(a) - set(b)
{'blue'}
>>> a
['red', 'blue', 'red'] # the lists are still the same

将它们转换为集合并进行集合减法

>>> a = ["red", "blue", "red"]
>>> b = ["yellow", "red"]
>>> set(a) - set(b)
{'blue'}
>>> a
['red', 'blue', 'red'] # the lists are still the same

结果列表中元素的顺序重要吗?如果没有,您可以将它们都转换为集合并找到集合差异
set(a)-set(b)
您可以使用:
[item for item in a If item not in b]
并将其包装到函数中,但我不知道是否有现成的内置解决方案结果列表中元素的顺序重要吗?如果没有,您可以将它们都转换为集合并找到集合差异
set(a)-set(b)
您可以使用:
[item for item in a If item not in b]
并将其包装到函数中,但我不知道是否有现成的内置解决方案