[英]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.