如何获取每一行和每一列的总和

时间:2019-04-29 15:43:39

标签: python

我很难展示每一行和每一列的合计值以及所有元素的合计。到目前为止,我拥有的代码会生成带有随机值的3x3表。我不知道如何获取每一行和每一列的总值。

import tkinter
import random

ROWS = 3
COLS = 3

class TableGUI:
    def __init__(self):
        self.main_window = tkinter.Tk()

        #Column
        self.labelC = []
        for i in range(COLS):
            self.labelC.append([])
            self.labelC[i] = tkinter.Label(self.main_window, width = 10,\
                    text = 'Column ' + str(i+1)).grid(row = 0, column = i+1)

        #Total label column
        self.labelC[i] = tkinter.Label(self.main_window, width = 10,\
                text = 'Total ').grid(row = 0, column = 4)

        #Row
        self.labelR = []

        for i in range(ROWS):
            self.labelR.append([])
            self.labelR[i] = tkinter.Label(self.main_window, width = 10,\
                    text = 'Row ' + str(i+1)).grid(row = i+1, column = 0)

        #Total label row
            self.labelR[i] = tkinter.Label(self.main_window, width = 10,\
                    text = 'Total ').grid(column = 0)


        #self.cells = [[0,0,0], [0,0,0]]
        self.cells = []
        for r in range(ROWS):
            self.cells.append([])
            for c in range(COLS):
                self.cells[r].append([])
                self.cells[r][c] = 0
        print(self.cells)

        self.vals = []
        for r in range(ROWS):
            self.vals.append([])    #Add a new row
            for c in range(COLS):
                self.vals[r].append([])
                self.vals[r][c] = tkinter.StringVar()
                self.vals[r][c].set(self.cells[r][c])

        #Entry
        self.entry = []
        for r in range(ROWS):
            self.entry.append([])
            for c in range(COLS):
                self.entry[r].append([])
                self.entry[r][c] = tkinter.Entry(self.main_window, width = 10,\
                        textvariable = self.vals[r][c]).grid(row = r+1,\
                        column = c+1)        

        #Make Table Button
        self.btnMR = tkinter.Button(self.main_window, \
                text = 'Make Table', command = self.makingTable).grid(
                    row = ROWS + 3, column = int(COLS/2 +1))

        tkinter.mainloop()

    def makingTable(self):
        for r in range(ROWS):
            for c in range(COLS):
                self.cells[r][c] = random.randint(1, 100)
                self.vals[r][c].set(self.cells[r][c])

my_gui = TableGUI()

这是我想要达到的预期结果的一个示例:

      Col1 Col2 Col3 Total
Row1   1     2    3    6
Row2   3     2    2    7
Row3   0     3    1    4
Total  4     7    6    17 

1 个答案:

答案 0 :(得分:0)

您可以将总和计算为

import functools

...
#initialization: a better way
self.cells = [[0]*COLS]*ROWS

...
#in makingTable
summary = [r + [sum(r)] for r in self.cells]
summary += [functools.reduce(lambda x, y: [xx+yy for xx,yy in zip(x,y)], summary)]

但是,如果您使用numpy,会更容易:

import numpy as np

...
#initialization
self.cells = np.zeros((ROWS, COLS), 'i')

...

#in makingTable
self.cells = np.random.uniform(0, 100, (ROWS, COLS)).astype('i')
summary = np.hstack((self.cells, np.sum(self.cells, 1, keepdims=True)))
summary = np.vstack((summary, np.sum(summary, 0)))