Python:执行此循环的 Pythonic 方式是什么?

标签 python

执行此循环的 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/

相关文章:

python - 使用假设重复条目的 Pandas Index 示例

python - sum() 不适用于 Timedelta 对象列表

python - 组合字符串,提取子字符串

python - Django- "python manage.py runserver"不工作

python - 在python中的类中定义一个方法

企业中的 Python : Pros and cons

python - 无法安装 whoosh alchemy

python - 创建一个总和为特定数字的数组?

python - 删除Python字典中的重复值

python - 在 Pandas 中读取 "length-delimited"文件?