本文整理汇总了Python中RandomArray.permutation方法的典型用法代码示例。如果您正苦于以下问题:Python RandomArray.permutation方法的具体用法?Python RandomArray.permutation怎么用?Python RandomArray.permutation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RandomArray
的用法示例。
在下文中一共展示了RandomArray.permutation方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: random_tree
# 需要导入模块: import RandomArray [as 别名]
# 或者: from RandomArray import permutation [as 别名]
def random_tree(labels):
"""
Given a list of labels, create a list of leaf nodes, and then one
by one pop them off, randomly grafting them on to the growing tree.
Return the root node.
"""
assert len(labels) > 2
import RandomArray; RandomArray.seed()
leaves = []
for label in labels:
leaves.append(Fnode(istip=1, label=label))
leaf_indices = list(RandomArray.permutation(len(leaves)))
joined = [leaves[leaf_indices.pop()]]
remaining = leaf_indices
while remaining:
i = RandomArray.randint(0, len(joined)-1)
c1 = joined[i]
if c1.back:
n = c1.bisect()
else:
n = InternalNode()
n.add_child(c1)
c = leaves[remaining.pop()]
n.add_child(c)
joined.append(c)
joined.append(n)
for node in joined:
if not node.back:
node.isroot = 1
return node