将二维数组转换为 [ [ x0 y0 ] [x1 y1] [x2 y2] ] 形式

问题描述

在 Python 中,我希望转换数组:

[[1,2,3,4,5],[10,20,30,40,50]]

进入

[[1 10][2 20][3 30][4 40][5 50]]

有没有简单的方法可以做到这一点?第二种数组的技术名称是什么?

不是

[[1,10][2,20][3,30][4,40][5,50]]

解决方法

>>> ans = []
>>> list1 = [1,2,3,4,5]
>>> list2 = [10,20,30,40,50]
>>> for a,b in zip(list1,list2):
...   ans.append([a,b])
... 
>>> ans
[[1,10],[2,20],[3,30],[4,40],[5,50]]
,

您可以使用 zip 获取结果

arr = [[1,5],[10,50]]
result = [list(x) for x in zip(*arr)]
,

试试这个

ls =[[1,50]]

res = [list(x) for x in zip(*ls)]

new_list = []
for i in res:
    item = ''.join(str(i).split(','))
    new_list.append(item)
print(new_list) #['[1 10]','[2 20]','[3 30]','[4 40]','[5 50]']

加入new_list后,它看起来像

print([''.join(new_list)]) #['[1 10][2 20][3 30][4 40][5 50]']
,

Python 中没有 MATLAB 的类似语法,您不能像这样定义列表或数组。

data := MyData{
    Inventory: inventory.GetInventory().String(),}

您必须用逗号分隔每个值。