Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 3.x 合并具有多行和多列的两个列表_Python 3.x_List_Matrix_Multiple Columns - Fatal编程技术网

Python 3.x 合并具有多行和多列的两个列表

Python 3.x 合并具有多行和多列的两个列表,python-3.x,list,matrix,multiple-columns,Python 3.x,List,Matrix,Multiple Columns,嗨,我有以下两个列表: list1 = [[(10.0, 10.0, 0.0), (90.0, 10.0, 0.0), (90.0, 90.0, 0.0), (10.0, 90.0, 0.0)]] list2 = [[(15.0, 7.0, 0.0), (40.0, 15.0, 0.0), (20.0, 30.0, 0.0), (11.0, 67.0, 0.0)]] 如何合并它们以使其看起来像: 10.0, 10.0, 0.0, 15.0, 7.0, 0.0 90.0, 10.0, 0.0,

嗨,我有以下两个列表:

list1 = [[(10.0, 10.0, 0.0), (90.0, 10.0, 0.0), (90.0, 90.0, 0.0), (10.0, 90.0, 0.0)]]
list2 = [[(15.0, 7.0, 0.0), (40.0, 15.0, 0.0), (20.0, 30.0, 0.0), (11.0, 67.0, 0.0)]]
如何合并它们以使其看起来像:

10.0, 10.0, 0.0, 15.0, 7.0, 0.0
90.0, 10.0, 0.0, 40.0, 15.0, 0.0
90.0, 90.0, 0.0, 20.0, 30.0, 0.0
10.0, 90.0, 0.0, 11.0, 67.0, 0.0

如果尝试不同的解决方案,但总是以错误或错误的表示结束。是否有一种方法可以分别处理每个元素?可以使用index?

numpy
模块,内置
zip()
itertools.chain。from\u iterable()
函数:

import itertools, numpy as np

list1 = [[(10.0, 10.0, 0.0), (90.0, 10.0, 0.0), (90.0, 90.0, 0.0), (10.0, 90.0, 0.0)]]
list2 = [[(15.0, 7.0, 0.0), (40.0, 15.0, 0.0), (20.0, 30.0, 0.0), (11.0, 67.0, 0.0)]]

chain = list(list(itertools.chain.from_iterable(i)) for i in zip(list1[0], list2[0]))
arr = np.array(chain, dtype=float)

print(arr)
输出:

[[ 10.  10.   0.  15.   7.   0.]
 [ 90.  10.   0.  40.  15.   0.]
 [ 90.  90.   0.  20.  30.   0.]
 [ 10.  90.   0.  11.  67.   0.]]

现在,您可以“分别处理每个元素”

假设我们希望从最后一行中获得4第th个值:

print(arr[3,3])  # 11.0

非常感谢。有没有办法分别处理每个元素?例如,我想比较第三行的第二个元素和第二行的第四个元素?谢谢,我知道了。关于
numpy
,特别是
np.insert
。有没有办法也插入str
result=np.insert(result,1,'G01',axis=1)
获取一个错误
value错误:无法将字符串转换为float:'G01'
@Rob In,若要处理字符串值,应使用适当的数据类型定义数组
dtype
arr=np.array(chain,dtype=str)
arr=np.array(chain,dtype=object)