[英]From nested tuples to edges of a binomial tree, in Python
def g(t):
res = []
for i,x in enumerate(t[:-1]):
res.append((x, t[-1][i]))
res.append((x, t[-1][i+1]))
return res
def h(t):
if isinstance((t[-1]), tuple):
return g(t) + h(t[-1])
else:
return []
t = (4, (3, 5, (2, 4, 6, (1, 3, 5, 7))))
print h(t)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.