考虑到我有这样一个列表(河流流量树): 字符串类型(11个元素)。你知道吗
From;To
1;2
2;3
5;4
3;4
4;-999
9;6
6;5
10;7
8;7
7;5
如果你把它想象成一棵树,它应该是(从上到下的方向):
1 9 8 10
| | \/
2 6 7
| \ /
3 5
| /
4
|
我只想把列表展开,这样我就可以得到
From;To
1;2
2;3
5;4
3;4
4;-999
9;6
6;5
10;7
8;7
7;5
1;3
1;4
6;4
9;4
9;5
7;4
8:4
8:5
10:5
10:4
树中必须有连接,顺序必须从上到下。你知道吗
最好的方法是什么? 我为此编写了一个代码,但这将花费我大约6天的时间来执行6000行。你知道吗
should_restart = False
for b in range(1, lengthchange):
row1 = str(tree[b])
position2 = row1.find(delimeter)
position3 = row1.find(end)
from1 = (row1[0:position2])
to1 = row1[position2+1:position3]
for c in range(1, lengthchange):
row2 = str(tree[c])
position4 = row2.find(delimeter)
position5 = row2.find(end)
from2 = (row2[0:position4])
to2 = row2[position4+1:position5]
if to1 == from2 and not to2 == "-999":
append1 = str(from1)+";"+str(to2)+"\n"
seen = set(tree)
if append1 not in seen:
seen.add(append1)
tree.append(append1)
should_restart = True
count_test = count_test+1
print(count_test)
lengthchange = len(tree)
你能查一下我的密码给我一些建议吗? 非常感谢你!你知道吗
因此,有效地做到这一点的关键是确保我们不必反复访问节点。我们可以从输出开始,然后返回:
如果
rivers
列表正确排序,则为O(n),如果未排序(或者在最坏的情况下,反向排序),则为O(n**2)。”在这种情况下,意味着它们在我们倒立的树中从根到叶被分类。。。因为我们的处理顺序是:4, 5, 3, 6, 7, 2, 9, 10, 8, 1
最终结果是:
可通过以下方式转换为最终格式:
相关问题 更多 >
编程相关推荐