我有一个 defaultdict fishparts = defaultdict(set) 有分配给它的元素,但使用 .clear() 定期清除我需要的是一些测试方法如果集合是否清晰,那么我可以在下面的函数中做一些其他工作。
def bandpassUniqReset5(player,x,epochtime):
score = 0
if lastplay[player] < (epochtime - 300):
fishparts[player].clear()
lastplay[player] = epochtime
for e in x:
# right here I want to do a check to see if the "if" conditional above has cleared fishparts[player] before I do the part below
if e not in fishparts[player]:
score += 1
fishparts[player].add(e)
return str(score)
请您参考如下方法:
与所有 Python 容器一样,集合在为空时被视为 False:
if not fishparts[player]:
# this set is empty
参见 Truth Value Testing .
演示:
>>> if not set(): print "I am empty"
...
I am empty
>>> if set([1]): print "I am not empty"
...
I am not empty
