[英]Subtracting sets in Python3 and getting output in list format
我有2套:
A = [2,2,2,3,5,4]
B = [2]
我想从A
删除所有 2。 我通过从A
减去B
来做到这一点,我需要列表格式的输出。 所以,我做了以下事情:
y = list(set(A) - set(B))
然而,它说:
TypeError: 'list' object is not callable
如果我使用y = list[set(CLI) - set(x)]
,它说:
TypeError: list indices must be integers or slices, not set
有什么建议,我怎么能得到列表格式的输出?
这也可以工作。
a = [2, 2, 2, 3, 5, 4]
b = [2]
def subtract_lists(a, b):
for i in b:
while i in a:
a.remove(i)
return a
print (subtract_lists(a, b))
输出 [3, 5, 4]
它在我的电脑上运行良好:
A = [2,2,2,3,5,4]
B = [2]
y = list(set(A) - set(B))
print(y)
输出:
[3, 4, 5]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.