Python 2.7 python中用于将项目列表拆分为列表(拆分函数)的代码

Python 2.7 python中用于将项目列表拆分为列表(拆分函数)的代码,python-2.7,Python 2.7,我有一个拆分列表的函数, 范例 def split(*arg): row = len(arg[0]) col = len(arg) new = [row * col] for i in row: for j in col: new[j][i] = arg[i][j] return new # this is method for split the list but it is include errors

我有一个拆分列表的函数, 范例

def split(*arg):
    row = len(arg[0])
    col = len(arg)
    new = [row * col]
    for i in row:
        for j in col:
            new[j][i] = arg[i][j]
    return new

    # this is method for split the list but it is include errors 
期望输出:

list_a = [(1,2,3),(8,9,10),(100,20,15)]

split (list_a) 
[(1,8,100),(2,9,20),(3,10,15)]
这很类似于

但是,您需要一个元组列表作为结果,因此我们甚至不需要列表理解。只是

list_a = [(1,2,3),(8,9,10),(100,20,15)]

zip(*list_a)  # Python 2
# or
list(zip(*list_a))  # Python 3
# [(1, 8, 100), (2, 9, 20), (3, 10, 15)]

这将使用和内置。

根据所需的输出,您似乎正在尝试查找,以便可以使用numpy执行以下操作:

import numpy
list_a = [(1,2,3),(8,9,10),(100,20,15)]
transpose_a = numpy.transpose(list_a)
print(transpose_a)
#or
#print(list(transpose_a))
但是您的
拆分
出现故障的原因如下:

  • 您正在使用但未解压参数,因此需要像调用
    split(*list_a)
  • new=[row*col]
    正在创建一个新列表,其中包含一个项目而不是一个项目
  • 您使用的不是
    范围(行)
    范围(列)
  • 行和列需要交换:
    row=len(arg)
    col=len(arg[0])
    ,因为您使用行作为第一维度,使用列作为第二维度

虽然我觉得这就是设计的目的,但也许你只需要用它来代替。

根据预期的输出,我想说你正在尝试找到