Python:what';执行这个循环的python方法是什么?

Python:what';执行这个循环的python方法是什么?,python,Python,执行此循环的Python方式是什么。我试图选择一个随机键,它将返回子树而不是根。因此:“parent==None”不能为true。“isRoot==True”也不能为True thekey = random.choice(tree.thedict.keys()) while (tree.thedict[thekey].parent == None)or(tree.thedict[thekey].isRoot == True): thekey = random.choice(tre

执行此循环的Python方式是什么。我试图选择一个随机键,它将返回子树而不是根。因此:“parent==None”不能为true。“isRoot==True”也不能为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())
.......
编辑:现在可以了

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

获取一个随机子树,该子树不是 根


获取一个随机子树,该子树不是 根

(在评论和问题版后更正)


(在评论和问题版后更正)

我认为这样更好:

theDict = tree.thedict

def getKey():
    return random.choice(theDict.keys())

theKey = getKey()

while theDict[thekey].parent in (None, True):
    thekey = getKey()

你觉得怎么样?

我觉得这样好一点:

theDict = tree.thedict

def getKey():
    return random.choice(theDict.keys())

theKey = getKey()

while theDict[thekey].parent in (None, True):
    thekey = getKey()
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
你觉得怎么样

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
或者对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)
或者对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)

我认为你的while条件有缺陷:

我想你应该这样想:
tree.thedict[thekey].parent==None

应等于以下值:
树。thedict[thekey].parent.isRoot==True

实际上,如果两者都表示“此节点不是根节点”,则应将第二条语句更改为:
tree.thedict[thekey].isRoot==True

如前所述,您的条件测试显示“当此节点是根节点或此节点的父节点是根节点时”。如果您的树结构是具有多个叶节点的单个根节点,那么在这种情况下,您应该期望有一个无限循环

这里有一个重写:

thekey = random.choice(k for k in tree.thedict.keys() if not k.isRoot)

我认为你的while条件有缺陷:

我想你应该这样想:
tree.thedict[thekey].parent==None

应等于以下值:
树。thedict[thekey].parent.isRoot==True

实际上,如果两者都表示“此节点不是根节点”,则应将第二条语句更改为:
tree.thedict[thekey].isRoot==True

如前所述,您的条件测试显示“当此节点是根节点或此节点的父节点是根节点时”。如果您的树结构是具有多个叶节点的单个根节点,那么在这种情况下,您应该期望有一个无限循环

这里有一个重写:

thekey = random.choice(k for k in tree.thedict.keys() if not k.isRoot)

就我个人而言,我不喜欢重复在while循环之前初始化key,然后在循环内部再次初始化。这可能是bug的来源;如果有人编辑两个初始化中的一个而忘记编辑另一个,会发生什么情况?即使这种情况从未发生,任何阅读代码的人都需要仔细检查以确保两个初始化完全匹配

我想这样写:

while True:
    thekey = random.choice(tree.thedict.keys())
    subtree = tree.thedict[thekey]
    if subtree.parent is not None and not subtree.isRoot:
        break
另外,如果您真的只想要子树,而不关心查找子树所需的键,您甚至可以这样做:

while True:
    subtree = random.choice(tree.thedict.values())
    if subtree.parent is not None and not subtree.isRoot:
        break
有些人可能不喜欢使用“
while True:
”,但这是Python的标准习惯用法,表示“永远循环,直到某个东西运行
中断”。这是一个简单、清晰、惯用的Python


p.p.S.这段代码实际上应该包装在一个if语句中,该语句检查树是否有多个节点。如果树只有一个根节点,这段代码将永远循环。

就我个人而言,我不喜欢在while循环之前初始化key,然后在循环内部再次初始化。这可能是bug的来源;如果有人编辑两个初始化中的一个而忘记编辑另一个,会发生什么情况?即使这种情况从未发生,任何阅读代码的人都需要仔细检查以确保两个初始化完全匹配

我想这样写:

while True:
    thekey = random.choice(tree.thedict.keys())
    subtree = tree.thedict[thekey]
    if subtree.parent is not None and not subtree.isRoot:
        break
另外,如果您真的只想要子树,而不关心查找子树所需的键,您甚至可以这样做:

while True:
    subtree = random.choice(tree.thedict.values())
    if subtree.parent is not None and not subtree.isRoot:
        break
有些人可能不喜欢使用“
while True:
”,但这是Python的标准习惯用法,表示“永远循环,直到某个东西运行
中断”。这是一个简单、清晰、惯用的Python



p.p.S.这段代码实际上应该包装在一个if语句中,该语句检查树是否有多个节点。如果树只有一个根节点,则此代码将永远循环。

您到底想做什么?了解dict
中值的类型将非常有用。获取一个不是根的随机子树。因此,如果父项为none,或者标志'isRoot'为True,我希望它获得另一个随机键,然后检查它。我们可能需要查看while循环的其余部分。到目前为止,您所拥有的似乎还不错,尽管它看起来像
parent==None
isRoot==True
在逻辑上应该是等价的。@Jweede:no,
assert
s用于记录程序执行过程中不可能出现的条件。您到底想做什么?了解ICT
中值的类型非常有用。获取一个不是根的随机子树。因此,如果父项为none,或者标志'isRoot'为True,我希望它获得另一个随机键,然后检查它。我们可能需要查看while循环的其余部分。到目前为止,您所拥有的似乎还不错,尽管它看起来像
parent==None
isRoot==True
在逻辑上应该是等价的。@Jweede:no,
assert
s用于记录程序执行期间不可能发生的条件。您否定了第一个条件测试。另外,
thekey.parent
没有意义。该键只是一个键,而不是树中的节点。我还认为他想知道dict[thekey]是否是根,而不是父项是否是根。@ntownsend,你说得对。我想知道键是否是根,而不是父项是否是根。您否定了第一个条件测试。而且,
thekey.parent
没有任何意义。该键只是一个键,而不是树中的节点。我还认为他想知道dict[thekey]是否是根,而不是父项是否是根。@ntownsend,你说得对。我想知道键是否是根,而不是父键是否是根。这完全不是一回事。这可能是从一个更大的循环中省略了循环体代码,这个循环对ICT起作用。没有更大的循环。这看起来非常简洁。这是一个非常昂贵的解决问题的方法。你是一个