<tfoot id='gMVHP'></tfoot>

        <bdo id='gMVHP'></bdo><ul id='gMVHP'></ul>

    1. <small id='gMVHP'></small><noframes id='gMVHP'>

    2. <legend id='gMVHP'><style id='gMVHP'><dir id='gMVHP'><q id='gMVHP'></q></dir></style></legend>
      <i id='gMVHP'><tr id='gMVHP'><dt id='gMVHP'><q id='gMVHP'><span id='gMVHP'><b id='gMVHP'><form id='gMVHP'><ins id='gMVHP'></ins><ul id='gMVHP'></ul><sub id='gMVHP'></sub></form><legend id='gMVHP'></legend><bdo id='gMVHP'><pre id='gMVHP'><center id='gMVHP'></center></pre></bdo></b><th id='gMVHP'></th></span></q></dt></tr></i><div id='gMVHP'><tfoot id='gMVHP'></tfoot><dl id='gMVHP'><fieldset id='gMVHP'></fieldset></dl></div>

        追加到列表列表中的一个列表也追加到所有其他列表

        时间:2023-09-27
          <i id='8QjMS'><tr id='8QjMS'><dt id='8QjMS'><q id='8QjMS'><span id='8QjMS'><b id='8QjMS'><form id='8QjMS'><ins id='8QjMS'></ins><ul id='8QjMS'></ul><sub id='8QjMS'></sub></form><legend id='8QjMS'></legend><bdo id='8QjMS'><pre id='8QjMS'><center id='8QjMS'></center></pre></bdo></b><th id='8QjMS'></th></span></q></dt></tr></i><div id='8QjMS'><tfoot id='8QjMS'></tfoot><dl id='8QjMS'><fieldset id='8QjMS'></fieldset></dl></div>

            <small id='8QjMS'></small><noframes id='8QjMS'>

            <legend id='8QjMS'><style id='8QjMS'><dir id='8QjMS'><q id='8QjMS'></q></dir></style></legend>
          • <tfoot id='8QjMS'></tfoot>

              • <bdo id='8QjMS'></bdo><ul id='8QjMS'></ul>
                  <tbody id='8QjMS'></tbody>

                  本文介绍了追加到列表列表中的一个列表也追加到所有其他列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对列表索引很生气,无法解释我做错了什么.

                  I'm getting mad with list indexes, and can't explain what I'm doing wrong.

                  我有这段代码,我想在其中创建一个列表列表,每个列表都包含我从 csv 读取的相同电路参数(电压、电流等)的值code> 文件如下所示:

                  I have this piece of code in which I want to create a list of lists, each one containing values of the same circuit parameter (voltage, current etc..) that I'm reading from a csv file that looks like this:

                  Sample, V1, I1, V2, I2
                  0, 3, 0.01, 3, 0.02
                  1, 3, 0.01, 3, 0.03
                  

                  等等.我想要的是创建一个列表,例如包含 V1 和 I1(但我想以交互方式选择)的形式 [[V1], [I1]],所以:

                  And so on. What I want is to create a list that for example contains V1 and I1 (but I want to chose interactively) in the form [[V1], [I1]], so:

                  [[3,3], [0.01, 0.01]]
                  

                  我使用的代码是这样的:

                  The code that I'm using is this:

                  plot_data = [[]]*len(positions)    
                  for row in reader:
                      for place in range(len(positions)):
                          value = float(row[positions[place]])
                          plot_data[place].append(value)
                  

                  plot_data 是包含所有值的列表,而 positions 是一个列表,其中包含我要从 .csv 复制的列的索引 文件.问题是,如果我在 shell 中尝试命令,似乎可以工作,但是如果我运行脚本而不是将每个值附加到正确的子列表,它会将所有值附加到所有列表,所以我获得 2(或更多) 相同的列表.

                  plot_data is the list that contains all the values, while positions is a list with the indexes of the columns that I want to copy from the .csv file. The problem is that if I try the commands in the shell, seems to work, but if I run the script instead of appending each value to the proper sub-list, it appends all values to all lists, so I obtain 2 (or more) identical lists.

                  推荐答案

                  Python 列表是可变对象,这里:

                  Python lists are mutable objects and here:

                  plot_data = [[]] * len(positions) 
                  

                  您正在重复相同的列表 len(positions) 次.

                  you are repeating the same list len(positions) times.

                  >>> plot_data = [[]] * 3
                  >>> plot_data
                  [[], [], []]
                  >>> plot_data[0].append(1)
                  >>> plot_data
                  [[1], [1], [1]]
                  >>> 
                  

                  列表中的每个列表都是对同一对象的引用.你修改一个,你会看到所有的修改.

                  Each list in your list is a reference to the same object. You modify one, you see the modification in all of them.

                  如果你想要不同的列表,你可以这样做:

                  If you want different lists, you can do this way:

                  plot_data = [[] for _ in positions]
                  

                  例如:

                  >>> pd = [[] for _ in range(3)]
                  >>> pd
                  [[], [], []]
                  >>> pd[0].append(1)
                  >>> pd
                  [[1], [], []]
                  

                  这篇关于追加到列表列表中的一个列表也追加到所有其他列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal() 下一篇:Python JSON转储/附加到.txt,每个变量都在新行

                  相关文章

                  <small id='vfuBE'></small><noframes id='vfuBE'>

                    <legend id='vfuBE'><style id='vfuBE'><dir id='vfuBE'><q id='vfuBE'></q></dir></style></legend>
                    <i id='vfuBE'><tr id='vfuBE'><dt id='vfuBE'><q id='vfuBE'><span id='vfuBE'><b id='vfuBE'><form id='vfuBE'><ins id='vfuBE'></ins><ul id='vfuBE'></ul><sub id='vfuBE'></sub></form><legend id='vfuBE'></legend><bdo id='vfuBE'><pre id='vfuBE'><center id='vfuBE'></center></pre></bdo></b><th id='vfuBE'></th></span></q></dt></tr></i><div id='vfuBE'><tfoot id='vfuBE'></tfoot><dl id='vfuBE'><fieldset id='vfuBE'></fieldset></dl></div>
                    • <bdo id='vfuBE'></bdo><ul id='vfuBE'></ul>
                      <tfoot id='vfuBE'></tfoot>