两个列表之间的差异大于两个列表中元素数量的差异

2024-09-27 21:29:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个已经处理的文件列表(A,B)。列表A包含所有初始文件,列表B包含所有已成功处理的文件(因此第二个列表(B)是第一个列表的子集)

A包含231453项

B包含124769项

我想减去它们,看看哪些文件没有得到处理(C应包含106684项)

为此,我使用设置

newlist=[]
newlist2=[]
newlist3=[]
newlist=( set(A) - ( set(A) & set(B) ) )
newlist2=(set(A)^set(B))
newlist3=(set(A) - set(B))
print len(newlist)
print len(newlist2)
print len(newlist3)

结果是:

134173
161662
134173

Why there are more items than the one expected?


Tags: 文件列表lenmoreitemsare子集there
2条回答

你的B包含一些不在A中的项如果B是A的子集那么所有三个长度应该是相同的。事实上,你的对称差异有更高的长度,你的B包含某些项目,而不是在A

您已经指定A和B是列表。列表中有可能有重复项在转换为set时丢失

A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.

为了你的案子你可以

not_processed = filter(lambda x: x in A, B)

或者

not_processed = [x for x in A if x in B]

如果x值在B中,上述代码将给出A中存在的所有x值

相关问题 更多 >

    热门问题