给定 set s,下面哪个段看起来更好?
if len(s) == 1:
v = s.copy().pop()
# important stuff using variable v takes place here
或者
if len(s) == 1:
v = s.pop()
s.add(v)
# important stuff using variable v takes place here
或者
if len(s) == 1:
for v in s:
# important stuff using variable v takes place here
我猜最后一段是最有效的,但是使用实际上从不循环的循环看起来不是很傻吗?
为什么 python 集没有替代方法来弹出不删除项目?
这似乎是一个微不足道的问题,但是当我多次遇到这种情况时,它已经变得需要抓挠了!