Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将二维数组转换为[[x0-y0][x1-y1][x2-y2]]形式_Python_Python 3.x - Fatal编程技术网

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

Python 将二维数组转换为[[x0-y0][x1-y1][x2-y2]]形式,python,python-3.x,Python,Python 3.x,在Python中,我希望转换数组: [[1, 2, 3, 4, 5],[10, 20, 30, 40, 50]] 进入 有没有一个简单的方法可以做到这一点?第二种阵列的技术名称是什么 不是 您可以使用zip获得结果 arr = [[1, 2, 3, 4, 5],[10, 20, 30, 40, 50]] result = [list(x) for x in zip(*arr)] 试试这个 ls =[[1, 2, 3, 4, 5],[10, 20, 30, 40, 50]] res = [l

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

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

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

不是


您可以使用
zip
获得结果

arr = [[1, 2, 3, 4, 5],[10, 20, 30, 40, 50]]
result = [list(x) for x in zip(*arr)]
试试这个

ls =[[1, 2, 3, 4, 5],[10, 20, 30, 40, 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]']
加入新的_列表后,它看起来像

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

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

A = [[1 2 3], [4 5 6]]

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

[list(i)for i in zip(*l)]
或者如果元组列表没有问题,只需
list(zip(*l))
。我更新了这个问题,但这个答案不是我想要的。基本上,我正在尝试将X和Y值的2D数组转换为与我正在处理的项目的示例代码中相同的格式。在示例代码中,列表是这样创建的:
X\u-train,X\u-test,y\u-train,y\u-test=train\u-test\u-split(X,y,test\u-size=0.2,random\u-state=123)
打印X\u-train,您将看到我的意思。什么是
[110]
<代码>[1,10]是一个包含两个元素的列表。我得到的输出中有逗号,我更新了问题。如果没有逗号,你想要一个字符串作为输出
ls =[[1, 2, 3, 4, 5],[10, 20, 30, 40, 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]']
print([''.join(new_list)]) #['[1 10][2 20][3 30][4 40][5 50]']
A = [[1 2 3], [4 5 6]]