执行此循环的 pythonic 方法是什么。我正在尝试选择一个将返回子树而不是根的随 secret 钥。因此:'parent == None' 不可能为真。 “isRoot==True”也不可能是真的。
thekey = random.choice(tree.thedict.keys())
while (tree.thedict[thekey].parent == None)or(tree.thedict[thekey].isRoot == True):
thekey = random.choice(tree.thedict.keys())
.......
编辑:现在有效
最佳答案
key = random.choice([key for key, subtree in tree.thedict.items()
if subtree.parent and not subtree.isRoot])
(评论和问题编辑后更正)
关于Python:执行此循环的 Pythonic 方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1411943/