Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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/3/arrays/14.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_Arrays_Python 3.x_List - Fatal编程技术网

Python 如何找到数组中每个列表的总和?

Python 如何找到数组中每个列表的总和?,python,arrays,python-3.x,list,Python,Arrays,Python 3.x,List,我要做的是找到数组中每个列表的单独总和,然后找到总和最大的列表 我尝试使用: np.sum(list) 问题是它将每个列表的总和相加,得到一个总和。例如: [[1,2,3,4],[5,6,7,8],[9,10,11,12]] #np.sum() would return 78 because it calculates 10+26+42 = 78 这就是我想要得到的: [[1,2,3,4],[5,6,7,8],[9,10,11,12]] #list1 = 10, list2 = 26, li

我要做的是找到数组中每个列表的单独总和,然后找到总和最大的列表

我尝试使用:

np.sum(list)
问题是它将每个列表的总和相加,得到一个总和。例如:

[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
#np.sum() would return 78 because it calculates 10+26+42 = 78
这就是我想要得到的:

[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
#list1 = 10, list2 = 26, list3 = 42
#The list with the max value is list3 with 42
这是我的密码:

#Sorry if this code is messy, I'm still new to this and it took me a few days to get here
#Basically this code takes a gird and finds the biggest area (i.e: width and height) of the grid

def FindAnswer(height, width, x, y, startx, starty):
  global origWidth, result

  #Find the values
  value = [row[startx:width] for row in plot[starty:height]]
  result.append(value)

  if width < x:
    #Raise the index im looking at and reset value
    startx += 1
    width += 1

    FindAnswer(height, width, x, y, startx, starty)

  elif height < y:
    #Reset width while going to a new row
    width = origWidth
    startx = 0

    #Go to a new row
    starty += 1
    height += 1

    FindAnswer(height, width, x, y, startx, starty)

plot = [[1, 2, 3, 4], 
     [5, 6, 7, 8], 
     [9, 10, 11, 12]]

result = []
#size of grid
x = 4 #amount of numbers in each list
y = 3 #number of rows

#Size of area I'm looking for
width = 1 #x >= width > 0
height = 2 #y >= height > 0
origWidth = width

startx = 0
starty = 0

FindAnswer(height, width, x, y, startx, starty)

print(result)
#如果这段代码很混乱,很抱歉,我还是新手,花了几天时间才来到这里
#基本上,该代码采用网格,并找到网格的最大面积(即:宽度和高度)
def FindAnswer(高度、宽度、x、y、startx、starty):
全局初始宽度、结果
#找到值
值=[starty:height]绘图中的行的[row[startx:WITH]
result.append(值)
如果宽度小于x:
#提高我正在查看的索引并重置值
startx+=1
宽度+=1
FindAnswer(高度、宽度、x、y、startx、starty)
elif高度=宽度>0
高度=2#y>=高度>0
origWidth=宽度
startx=0
starty=0
FindAnswer(高度、宽度、x、y、startx、starty)
打印(结果)
尝试使用:

np.sum(l, axis=1)
或者您@furas的答案,或者使用:

print(list(map(sum, l)))

list
更改为
l
,因为这将覆盖
list
关键字。

使用列表理解,您可以计算所有值

data = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]

new = [sum(x) for x in data]

print(new)

[10, 26, 42]

如果允许您使用numpy,那么您也可以通过以下方式进行操作:

ans = []
for lst in arr1:
    ans.append(np.sum(lst))
print(ans)

[10, 26, 42]