Python:列表附加问题

标签 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]]

随着新项目的附加,旧项目被新项目取代。这种趋势仍在继续。 谁能告诉我为什么会这样。我曾尝试使用“追加”代替插入,但得到了相同的输出。但是,当我使用“扩展”时,我得到了正确的输出,但我需要内部项目作为列表,而扩展无法获得。

最佳答案

您需要在循环开始时重新绑定(bind)cumulative_nodes_found_list,而不是仅仅清除它。

关于Python:列表附加问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2171551/

相关文章:

python - 找不到有效的 Python 解释器。请在您的 LaTeXTools 设置中设置 python 设置

python - django导入导出应用程序错误: django. core.exceptions.AppRegistryNotReady:模型尚未加载

python - Pandas - 从其中一列将列添加到基于 Dict 的 DataFrame

python - 在 Windows 10 上使用 pip 安装 numpy for python 3.7

python - 根据条件填充数据框行的值

python - Pandas Dataframe CSV 导出,如何防止额外的双引号字符

python - 如果我重用它们,我应该缓存范围结果吗?

python - python列表中的不连续切片

python - 在Python中生成一个名为当前时间的文本文件

python - 迭代相同的 freezeset 是否保证始终以相同的顺序生成项目? (Python 3)