你好,我已经编码了几个月并掌握了基础知识,但是我遇到了一个固定会员问题,找不到解决方法。
我有一系列整数对的列表,我想删除其中包含“a”整数的列表。我认为使用集合是最简单的方法。下面是代码:
## This is the item to test against.
a = set([3])
## This is the list to test.
groups = [[3, 2], [3, 4], [1, 2], [5, 4], [4, 3]]
## This is a list that will contain the lists present
## in groups which do not contain "a"
groups_no_a = []
for group in groups:
group = set(group)
if a in group:
groups_no_a.append(group)
## I thought the problem had something to do with
## clearing the variable so I put this in,
## but to no remedy.
group.clear()
print groups_no_a
我之前尝试使用 s.issubset(t)
,直到意识到这个方法测试的是 s
中的每一个元素是否在 t
中。
谢谢!
one_element in somelist
必须扫描列表。 - Martijn Pieters