![](/img/trans.png)
[英]Adding first occurrence in a 2D list to another list (Python 3.4.2)
[英]adding another value to 2d list
在 Python 中,我正在編寫一個卡方函數來分析圖片。 這是當前函數,它獲取每個像素的 RGB 值並將其放入列表中:
`def chiSquared(c):
counter=0
width, height = c.size
matrix = [[0 for x in range(3)] for y in range(height*width)]
for x in range(width):
for y in range(height):
matrix[counter][0]=c.getpixel((x,y))[0]
matrix[counter][1]=c.getpixel((x,y))[1]
matrix[counter][2]=c.getpixel((x,y))[2]
counter+=1`
矩陣看起來像:
[[230, 222, 218], [221, 218, 210], [218, 214, 206], [213, 206, 202], [214, 210, 206], [217, 215, 206], [218, 214, 210], [222, 218, 210], [225, 218, 214], [230, 222, 214], [221, 214, 206], [213, 211, 207], [205, 202, 198], [198, 194, 186], [177, 170, 166]...
我想知道的是:是否有一種簡單的方法可以將每個 3 元組中的值相加並創建一個 4 元組,其中最后一位數字是 3 元組的總和。 上述矩陣的 IE -> [230,222,218] -> [230,222,218,670]
編輯1:簡單我的意思是有更好的方法然后對整個矩陣(每個3元組)求和並創建一個新矩陣。
國際大學聯盟
l=[[230, 222, 218], [221, 218, 210]]
[x+[sum(x)] for x in l]
Out[17]: [[230, 222, 218, 670], [221, 218, 210, 649]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.