[英]Python: what's the pythonic way to perform this loop?
What is the pythonic way to perform this loop. 执行此循环的pythonic方法是什么。 I'm trying to pick a random key that will return a subtree and not the root.
我试图选择一个随机键,它将返回一个子树而不是根。 Hence: 'parent == None' cannot be true.
因此:'parent == None'不能为真。 Nor can 'isRoot==True' be true.
'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())
.......
edit: it works now 编辑:现在可以使用
get a random subtree that is not the root
得到不是根的随机子树
not_root_nodes = [key, node for key,node in tree.thedict.iteritems() if not ( node.parent is None or node.isRoot)]
item = random.choice( not_root_nodes )
key = random.choice([key for key, subtree in tree.thedict.items()
if subtree.parent and not subtree.isRoot])
(Corrected after comments and question edition) (在评论和问题版后更正)
thekey = random.choice(tree.thedict.keys())
parent = thedict[thekey].parent
while parent is None or parent.isRoot:
thekey = random.choice(tree.thedict.keys())
parent = thedict[thekey].parent
I think that's a bit better: 我认为这要好一些:
theDict = tree.thedict
def getKey():
return random.choice(theDict.keys())
theKey = getKey()
while theDict[thekey].parent in (None, True):
thekey = getKey()
What do you think? 你怎么看?
def is_root(v):
assert (v.parent != None) == (v.isRoot)
return v.isRoot
#note how dumb this function looks when you guarantee that assertion
def get_random_nonroot_key():
while True:
thekey = random.choice(tree.thedict.keys())
value = tree.thedict[thekey]
if not is_root(value): return key
or a refactoring of Roberto Bonvallet's answer 或重构Roberto Bonvallet的答案
def get_random_nonroot_key():
eligible_keys = [k for k, v in tree.thedict.items() if not is_root(v)]
return random.choice(eligible_keys)
I think your while condition is flawed: 我认为您的情况有缺陷:
I think you expect this: tree.thedict[thekey].parent == None
我认为您期望这样:
tree.thedict[thekey].parent == None
should be equal to this: tree.thedict[thekey].parent.isRoot == True
应该等于:
tree.thedict[thekey].parent.isRoot == True
When in fact, for both to mean "this node is not the root", you should change the second statement to: tree.thedict[thekey].isRoot == True
实际上,对于两者都意味着“此节点不是根”的情况,应将第二条语句更改为:
tree.thedict[thekey].isRoot == True
As written, your conditional test says "while this node is the root OR this node's parent is the root". 如所写,您的条件测试说“此节点是根节点或此节点的父节点是根节点”。 If your tree structure is a single root node with many leaf nodes, you should expect an infinite loop in this case.
如果树结构是具有许多叶节点的单个根节点,则在这种情况下,您应该期待一个无限循环。
Here's a rewrite: 这是一个重写:
thekey = random.choice(k for k in tree.thedict.keys() if not k.isRoot)
thekey = random.choice(tree.thedict.keys())
parent = tree.thedict[thekey].parent
while parent is None or tree.thedict[thekey].isRoot:
thekey = random.choice(tree.thedict.keys())
parent = thedict[thekey].parent
Personally, I don't like the repetition of initializing thekey before the while loop and then again inside the loop. 就个人而言,我不喜欢重复在while循环之前初始化键,然后在循环内部再次初始化键的重复。 It's a possible source of bugs;
这可能是错误的来源。 what happens if someone edits one of the two initializations and forgets to edit the other?
如果有人编辑两个初始化之一而忘记编辑另一个初始化,会发生什么? Even if that never happens, anyone reading the code needs to check carefully to make sure both initializations match perfectly.
即使从未发生过,阅读代码的任何人都需要仔细检查以确保两个初始化完全匹配。
I would write it like so: 我会这样写:
while True:
thekey = random.choice(tree.thedict.keys())
subtree = tree.thedict[thekey]
if subtree.parent is not None and not subtree.isRoot:
break
PS If you really just want the subtree, and don't care about the key needed to lookup the subtree, you could even do this: PS:如果您真的只想要子树,而不关心查找子树所需的键,则可以执行以下操作:
while True:
subtree = random.choice(tree.thedict.values())
if subtree.parent is not None and not subtree.isRoot:
break
Some people may not like the use of " while True:
" but that is the standard Python idiom for "loop forever until something runs break
". 有些人可能不喜欢使用“
while True:
”但是这是标准的Python成语“永远循环下去,直到有运行break
”。 IMHO this is simple, clear, idiomatic Python. 恕我直言,这是简单,清晰,惯用的Python。
PPS This code should really be wrapped in an if statement that checks that the tree has more than one node. PPS该代码实际上应该包装在if语句中,该语句检查树是否具有多个节点。 If the tree only has a root node, this code would loop forever.
如果树只有一个根节点,则此代码将永远循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.