Python:list append的问题

2024-10-01 22:39:40 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码-

cumulative_nodes_found_list = []
cumulative_nodes_found_total_list = []

no_of_runs = 10

count = 0

while count < no_of_runs:

 #My program code

 print 'cumulative_nodes_found_list - ' + str(cumulative_nodes_found_list)
 cumulative_nodes_found_total_list.insert(count,cumulative_nodes_found_list)
 print 'cumulative_nodes_found_total_list - ' + str(cumulative_nodes_found_total_list)
 count = count + 1

这是输出的一部分-

#count = 0
cumulative_nodes_found_list - [0.0, 0.4693999, 0.6482, 0.6927999999, 0.7208999999, 0.7561999999, 0.783399999, 0.813999999, 0.8300999999, 0.8498, 0.8621999999]

cumulative_nodes_found_total_list - [[0.0, 0.4693999, 0.6482, 0.6927999999, 0.7208999999, 0.7561999999, 0.783399999, 0.813999999, 0.8300999999, 0.8498, 0.8621999999]]

#count = 1
cumulative_nodes_found_list - [0.0, 0.55979999999999996, 0.66220000000000001, 0.69479999999999997, 0.72040000000000004, 0.75380000000000003, 0.77629999999999999, 0.79679999999999995, 0.82979999999999998, 0.84850000000000003, 0.85760000000000003]

cumulative_nodes_found_total_list -[[0.0, 0.55979999999999996, 0.66220000000000001, 0.69479999999999997, 0.72040000000000004, 0.75380000000000003, 0.77629999999999999, 0.79679999999999995, 0.82979999999999998, 0.84850000000000003, 0.85760000000000003], 
[0.0, 0.55979999999999996, 0.66220000000000001, 0.69479999999999997, 0.72040000000000004, 0.75380000000000003, 0.77629999999999999, 0.79679999999999995, 0.82979999999999998, 0.84850000000000003, 0.85760000000000003]]

当新项被追加时,旧项将被新项替换。这种趋势还在继续。 有人能告诉我为什么会这样吗。我试过用'append'代替insert,但得到了相同的输出。然而,当我使用“扩展”时,我得到了正确的输出,但我需要内部项目作为列表,而不是使用扩展。


Tags: ofno密码mycountrunslisttotal

热门问题