在Python中为元组(for循环中的zip())添加值

时间:2016-08-25 01:41:54

标签: python numpy

我需要将列表列表格式化为元组列表,所需的结果是:

<ColorAnimation Storyboard.TargetName="ball" 
                Storyboard.TargetProperty="Fill.(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)"
                From="Red" To="LightGreen" Duration="0:0:0.3"/>

我想要的输出是:

location /images/ {
   try_files $uri /images/default.gif;
}

现在想象一下,列表A非常大,我不能编写一小段代码,如果我尝试循环zip()函数,结果如下:

>>A = [[1, 2, 3]]
>>A.append([1, 2, 3])
>>A.append([1, 2, 3])
>>print A
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]

我提出的令人讨厌的解决方案是使用递归循环:

>>zip (A[0],A[1],A[2])
[(1, 1, 1), (2, 2, 2), (3, 3, 3)]

返回所需的结果:

>>print zip (A[0],a)
>>[(1, (1, 1, 1)), (2, (2, 2, 2)), (3, (3, 3, 3))]

因此,是否有适当/更清洁/更快的方法来解决这个问题?

(因为它是Python的嵌入式包我不能使用Pandas坚果NumPy在早期版本中被允许)

1 个答案:

答案 0 :(得分:2)

尝试zip(*A),这正是zip(A[0],A[1],...)

(对于任何功能都是如此,而不仅仅是拉链)。 https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists