python| how can I convert a 2d array to an array of pairs

时间:2021-06-14 09:22:33

So I have a 2D array (below)

所以我有一个2D数组(下图)

array([[ 1,  1],
       [  1,   1 ]]

That I would like to convert into an array of paris (lon, lat) just as below

我想将其转换为巴黎(lon,lat)数组,如下所示

[(1, 1), (1, 1)]

[(1,1),(1,1)]

How can I do that ?

我怎样才能做到这一点 ?

3 个解决方案

#1


1  

This can be done by a list comprehension:

这可以通过列表理解来完成:

array = [[ 1,  1], [ 1,  1 ]]

list_of_tuples = [(x,y) for x,y in array]

A working example can be found here.

可以在这里找到一个工作示例。

#2


1  

a = np.array([[ 1,  1],
       [  1,   1 ]])

a
Out[31]: 
array([[1, 1],
       [1, 1]])

#Iterate the array and convert each element to a tuple.
[tuple(e) for e in a]
Out[32]: [(1, 1), (1, 1)]

#3


0  

This has worked for me. Thanks @Divakar for the helpful comment

这对我有用。感谢@Divakar的有用评论

list(map(tuple,arr))

#1


1  

This can be done by a list comprehension:

这可以通过列表理解来完成:

array = [[ 1,  1], [ 1,  1 ]]

list_of_tuples = [(x,y) for x,y in array]

A working example can be found here.

可以在这里找到一个工作示例。

#2


1  

a = np.array([[ 1,  1],
       [  1,   1 ]])

a
Out[31]: 
array([[1, 1],
       [1, 1]])

#Iterate the array and convert each element to a tuple.
[tuple(e) for e in a]
Out[32]: [(1, 1), (1, 1)]

#3


0  

This has worked for me. Thanks @Divakar for the helpful comment

这对我有用。感谢@Divakar的有用评论

list(map(tuple,arr))