每个列表的元素成一个数组

时间:2015-11-04 14:01:26

标签: python python-2.7

我有一个清单说

A = [[[1], [2]],
     [[3], [4]],
     [[5], [6]],
     [[7], [8]],
     [[9], [10]],
     [[11], [12]]]

现在我想转换成类似这样的数组

A = [[1,3,5,7,9,11],[2,4,6,8,10,12]]

将每个列表的元素放入数组

My real data looks something like this,
    [[], []]
    [[], []]
    [[], []]
    [[], []]
    [[], []]
    [[], []]
    [[2.9077100703958676, 2.907707020607246, 2.907696129398862, 2.9076922793200826], [1.5831895878032682, 1.5831799549447967, 1.5421635176438928, 1.54216257550671]]
    [[], [3.6722596549808784, 3.6722526678432876, 3.671068320950512, 3.6710584810218814, 1.4814967342403382, 1.4814744077628512, 1.158833238469524, 1.158820971788903]]
    [[], []]
    [[], []]
    [[], []]
    [[], []]
    [[], []]
    [[], []]
    [[], [11.161297964771585]]
    [[], []]
    [[], []]
    [[], []]
    [[], [1.7247699149091442, 1.7247456715167526, 1.3559139927513728, 1.3559021903808808]]
    [[], []]
    [[], []]
    [[0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5]]
    [[], []]
    [[], []]
    [[], []]
    [[], [53.3587073998281, 53.3587073998281]]
    [[], []]
    [[], []]
    [[], [73.51669471981023]]
    [[], [12.93677609502523, 12.936752858061059]]
    [[], [20.935136118658637]]

这是我打印变量

时得到的结果

我们有什么方法可以做到这一点吗?

2 个答案:

答案 0 :(得分:0)

如评论中所述,您对A的定义无效。

假设您的意思是A = [[1,2],[3,4],...],您可以这样做:

[[x[0][0] for x in A ], [x[1][0] for x in A]]

答案 1 :(得分:0)

$this->lang->load('lang_file', $this->session->userdata('lang'))

编辑:假设下面的2D列表的原始答案:

>>> A = [[[1], [2]],[[3], [4]],[[5], [6]],[[7], [8]],[[9], [10]],[[11], [12]]] # ouch.
>>> [list([item[0] for item in tuple]) for tuple in zip(*A)]
[[1, 3, 5, 7, 9, 11], [2, 4, 6, 8, 10, 12]]

或者,如果是