【问题标题】:Python: copying chosen rows form list to anotherPython:将选定的行从列表复制到另一个
【发布时间】:2016-06-03 10:57:46
【问题描述】:

我正在尝试编写一个函数,将所选行从列表复制到另一个,基于第三个列表应该采取的点。 “1” - 用这个东西复制行,“0”省略。 这是我的代码:

stuff = [
[1, 2, 3],
[10, 20, 30],
[100, 200, 300],
[1000, 2000, 3000],
[10000, 20000, 30000],
]

chooseThese = [1,0,1,1,0]

def fnChoose(arr1D, arr):
    new = []
    for digit in arr1D:
        for row in arr:
            if arr1D[digit] == 1:
                new.append(arr[row])
            else:
                continue
    return new


print (fnChoose(chooseThese, stuff))

结果我不想得到:

[[1, 2, 3], [100, 200, 300], [1000, 2000, 3000]]

不幸的是我的功能不起作用,空闲显示以下错误:

Traceback (most recent call last):
  File "~\file.py", line 21, in <module>
    fnChoose(chooseThese, stuff)
  File "~\file.py", line 16, in fnChoose
    new.append(arr[row])
TypeError: list indices must be integers or slices, not list

我应该如何纠正这个功能?如何将整行追加到列表中?

【问题讨论】:

    标签: python list copying


    【解决方案1】:

    更简单的方法是使用 索引列表 而不是 0/1 列表,使用 enumerate 创建一个索引理解或使用组合使用zip 列出:

    stuff = [
    [1, 2, 3],
    [10, 20, 30],
    [100, 200, 300],
    [1000, 2000, 3000],
    [10000, 20000, 30000],
    ]
    
    chooseThese = [1,0,1,1,0]
    
    # use enumerate (two variants)
    new = [s for index, s in enumerate(stuff) if chooseThese[index] == 1]
    print(new)
    new = [stuff[index] for index, choose in enumerate(chooseThese) if choose == 1]
    print(new)
    
    # use zip
    new = [s for choose, s in zip(chooseThese, stuff) if choose == 1]
    print(new)
    
    # use indexed chooser-variable
    chooseThese = [0,2,3]
    new = [stuff[index] for index in chooseThese]
    print(new)
    

    【讨论】:

      【解决方案2】:

      您应该跟踪您尝试从中追加的列表的索引。

      stuff = [                                                                              
           [1, 2, 3],                                                                    
           [10, 20, 30],                                                                 
           [100, 200, 300],                                                              
           [1000, 2000, 3000],                                                           
           [10000, 20000, 30000],                                                        
           ]                                                                             
      chooseThese = [1,0,1,1,0]                                                              
      
      def fnChoose(arr1D, arr):                                                              
          new = []                                                                           
          index = 0                                                                          
          for row in arr:                                                                    
              if (arr1D[index] == 1):                                                        
                  new.append(row)                                                            
              index += 1                                                                     
          return new                                                                         
      
      print (fnChoose(chooseThese, stuff))  
      

      【讨论】:

        猜你喜欢
        • 2011-07-06
        • 2012-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-06
        • 2012-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多