我如何找到数组中每个单独列表的总和? - python

我想做的是找到数组中每个列表的单个和,然后找到具有最大和的列表。

我尝试使用:

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)

参考方案

尝试使用:

np.sum(l, axis=1)

或者您@furas的答案,或使用:

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

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

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

Python:图像处理可产生皱纹纸效果 - python

也许很难描述我的问题。我正在寻找Python中的算法,以在带有某些文本的白色图像上创建皱纹纸效果。我的第一个尝试是在带有文字的图像上添加一些真实的皱纹纸图像(具有透明度)。看起来不错,但副作用是文本没有真正起皱。所以我正在寻找更好的解决方案,有什么想法吗?谢谢 参考方案 除了使用透明性之外,假设您有两张相同尺寸的图像,一张在皱纹纸上明亮,一张在白色背景上有深…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Python:无法识别Pip命令 - python

这是我拍摄的屏幕截图。当我尝试在命令提示符下使用pip时,出现以下错误消息:pip无法识别为内部或外部命令,可操作程序或批处理文件。我已经检查了这个线程:How do I install pip on Windows?我所能找到的就是我必须将"C:\PythonX\Scripts"添加到我的类路径中,其中X代表python版本。如您在我的…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…