嵌套数组到具有子集/元组的单个数组

时间:2013-07-16 20:49:28

标签: python dataformat

所以我在这种格式的数组中有数据:

[[x_1,y_1],...,[x_n,y_n]]

我需要这种格式:

[(x_1,y_2),...,(x_n,y_n)]

1 个答案:

答案 0 :(得分:3)

对此tuple()的简单调用就足够了。

>>> testList = [[1, 2], [1, 3]]
>>> [tuple(elem) for elem in testList]
[(1, 2), (1, 3)]

或者,如果您希望使用map,请执行

>>> map(tuple, testList)
[(1, 2), (1, 3)]