我有一个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)
Run Code Online (Sandbox Code Playgroud)
与所有Python容器一样,集合在空时被视为False:
if not fishparts[player]:
# this set is empty
Run Code Online (Sandbox Code Playgroud)
请参阅真值测试.
演示:
>>> if not set(): print "I am empty"
...
I am empty
>>> if set([1]): print "I am not empty"
...
I am not empty
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3763 次 |
| 最近记录: |