Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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/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不能进行矩阵求和_Python_List_Sum - Fatal编程技术网

Python不能进行矩阵求和

Python不能进行矩阵求和,python,list,sum,Python,List,Sum,我对Python的矩阵生成功能有点奇怪的问题 我有以下清单: Test01=[11,25,53,19] Test02=[2,6,4,8] 我想编写一个代码,将每个列表中的每个元素添加到如下所示: 11 + 2, 11 + 6, 11 + 4, 11+ 8 25 + 2, 25 + 6 等等等等 下面是我编写的代码 test01中i的: c=[test02中x的i+x] 印刷品(c) 这是我从中得到的结果(也是我想要的) 现在我有了一个大数据集,并做了一些计算。我选择的前两行进行相同的计算

我对Python的矩阵生成功能有点奇怪的问题

我有以下清单:

Test01=[11,25,53,19]
Test02=[2,6,4,8]
我想编写一个代码,将每个列表中的每个元素添加到如下所示:

11 + 2, 11 + 6, 11 + 4, 11+ 8

25 + 2, 25 + 6
等等等等

下面是我编写的代码

test01中i的
:
c=[test02中x的i+x]
印刷品(c)
这是我从中得到的结果(也是我想要的)

现在我有了一个大数据集,并做了一些计算。我选择的前两行进行相同的计算(即相同类型的矩阵)。 但是,Python只添加了这样的内容:

New_list01=1,2,3,4
新列表02=a、b、c、d
[1+a、2+b、3+c、4+d]]
只有一行,而不是预期的四行 为什么会这样? 我将列表转换为numpy(.to_numpy)和to to list(.tolist())。仍然是相同的(错误的)答案。 我怎么还能算出我想要的呢

谢谢你的帮助!
(顺便说一句,如果有一个内置函数可以执行我正在寻找的任务,那就更好了。)

我认为在函数按预期工作时,如何使用函数存在问题

您可以使用Numpy,然后使用循环并将值传递给第二个矩阵

import numpy as numpy 
    
matrix1 = numpy.array([2, -7, 5]) 
matrix2 = numpy.array([5, 8, -5]) 

total = numpy.add(matrix1, matrix2)  
print ("output added array : ", total)  
输出:

output added array :  [7 1 0]
 output added array :  
         [[ 7 10 -3]
         [-2  1 -2]
         [10 13  0]]
您还可以将其用于多维数组

import numpy as numpy
martix1 = numpy.array([[2, 2, 2], [-7, -7, -7], [5, 5, 5]])
matrix2 = numpy.array([[5, 8, -5], [5, 8, 5], [5, 8, -5]])

total = numpy.add(martix1, matrix2)
print("output added array : \n", total)
输出:

output added array :  [7 1 0]
 output added array :  
         [[ 7 10 -3]
         [-2  1 -2]
         [10 13  0]]

非常感谢您的回复。我问了一位同事,他帮我解决了这个问题,具体如下:

#列表列表
列表中的列表=[范围(4),范围(7)]
扁平化列表=[]
#将列表展平
对于\u列表中的\u列表中的x:
对于x中的y:
展平列表。追加(y)

问题是列表中有列表,需要将其展平,只需在一行中使用列表理解即可。不确定您到底想要什么输出,因此添加了两个选项:

>>> New_list01 = [1, 2, 3, 4]
>>> New_list02 = ['a','b','c','d']

>>> out = [[i+str(j) for i in New_list02] for j in New_list01]
>>> print(out)
[['a1', 'b1', 'c1', 'd1'], 
['a2', 'b2', 'c2', 'd2'], 
['a3', 'b3', 'c3', 'd3'], 
['a4', 'b4', 'c4', 'd4']]

>>> out = [[i+str(j) for i in New_list02] for j in New_list01]
>>> print(out)
[['a1', 'b1', 'c1', 'd1'], 
['a2', 'b2', 'c2', 'd2'], 
['a3', 'b3', 'c3', 'd3'], 
['a4', 'b4', 'c4', 'd4']]

>>> out = [[str(j) + i for i in New_list02] for j in New_list01]
[['1a', '1b', '1c', '1d'], 
['2a', '2b', '2c', '2d'], 
['3a', '3b', '3c', '3d'], 
['4a', '4b', '4c', '4d']]

>>> out = [[str(j) + '+' + i for i in New_list02] for j in New_list01]
[['1+a', '1+b', '1+c', '1+d'], 
['2+a', '2+b', '2+c', '2+d'], 
['3+a', '3+b', '3+c', '3+d'], 
['4+a', '4+b', '4+c', '4+d']]

>>> out = [[i + '+' + str(j) for i in New_list02] for j in New_list01]
[['a+1', 'b+1', 'c+1', 'd+1'], 
['a+2', 'b+2', 'c+2', 'd+2'], 
['a+3', 'b+3', 'c+3', 'd+3'], 
['a+4', 'b+4', 'c+4', 'd+4']]

>>> # Filtering out one row
>>> out[0] 
['a+1', 'b+1', 'c+1', 'd+1']

如果要展平
输出(一个列表)

>>>  just remove inner []
>>> out = [i+ '+' + str(j) for i in New_list02 for j in New_list01]
>>> print(out)
['a+1', 'a+2', 'a+3', 'a+4', 'b+1', 'b+2', 'b+3', 'b+4', 'c+1', 'c+2', 'c+3', 'c+4', 'd+1', 'd+2', 'd+3', 'd+4']

>>> out = [i + str(j) for i in New_list02 for j in New_list01]
>>> print(out)
['a1', 'a2', 'a3', 'a4', 'b1', 'b2', 'b3', 'b4', 'c1', 'c2', 'c3', 'c4', 'd1', 'd2', 'd3', 'd4']
对于初始测试用例

>>> out = [[i+j for i in Test02] for j in Test01]
>>> print(out)
[[13, 17, 15, 19], 
[27, 31, 29, 33], 
[55, 59, 57, 61], 
[21, 25, 23, 27]]

>>> #first row
>>> print(out[0])
[13, 17, 15, 19]

>>> out = [[i+j for i in Test02] for j in Test01]
[13, 27, 55, 21, 17, 31, 59, 25, 15, 29, 57, 23, 19, 33, 61, 27]

你能告诉我们这个大数据集是什么样子的吗?您提供的代码工作正常。您好@Countour Integral,谢谢您的回复。列为0.8*房价值、0.6*房价值等,因此每个“单元格”的值大于10万美元。然后我取每列的平均值,留下四个值(以熊猫为单位)。然后选择Pandas中的第一行,并将四个值转换为一个列表。但是,它也会产生双方括号。以上信息有用吗?我已经添加了一个答案。让我知道它是否对你有效。谢谢侯赛因,我正在寻找以下输出:
[2+5,2+8,2+-5]
[-7+5,-7+8,-7+5]
[5+5,5+8,5+-5]
,然后我可以输出到CSV。这样回答了你的问题吗?不,我只是礼貌地感谢你的时间和努力。我的问题没有得到回答。我不想要一行,而是一个矩阵。因此,每个列表中的每个元素都将与另一个列表中的每个元素求和。如果有道理的话我希望这能回答你的问题不,让我用不同的方式来解释:Input list01=abcdlist02=fghj期望的输出a+fb+fc+fd+fa+gb+gc+gd+ga+hb+hc+hd+ha+jb+jc+jd+jcheckout我更新的答案,如果你想要一个平坦的输出。