我对我遇到的问题感到有些困惑,并想知道是否有人可以提供帮助(这在我看来似乎微不足道,所以我希望它真的是!)
基本上,我通过以下列表理解按列表过滤:
depfilt = [s for s in department if 'author' not in s]
Run Code Online (Sandbox Code Playgroud)
(部门有154个元素,最终的depfilt有72个元素)
现在,我还有一个单独的iD值列表,其中包含154个元素(subj),这些列表的索引与其中的索引相匹配department.我希望在过滤过程后保留正确的iD值,因此使用以下代码行:
subfilt = [s for s in subj if 'author' not in department[subj.index(s)]]
Run Code Online (Sandbox Code Playgroud)
在我看来,我觉得这应该有效,但是subfilt实际上返回了106个列表元素,而不是72个.
有人知道为什么吗?
谢谢
使用enumerate而不是index重复值的情况
[s for i, s in enumerate(subj) if 'author' not in department[i]]
Run Code Online (Sandbox Code Playgroud)