首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何对2D列表中的列求和?

对2D列表中的列求和可以通过使用Python的嵌套循环来实现。首先,我们需要确定2D列表中的行数和列数,然后使用嵌套循环遍历每个元素并将其值累加到对应列的总和中。以下是一个示例代码:

代码语言:python
代码运行次数:0
复制
def column_sums(matrix):
    row_count = len(matrix)
    col_count = len(matrix[0])
    col_sums = [0] * col_count

    for row in matrix:
        for col_idx, val in enumerate(row):
            col_sums[col_idx] += val

    return col_sums

这个函数接受一个2D列表作为输入,并返回一个列表,其中包含每列的总和。例如:

代码语言:python
代码运行次数:0
复制
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

col_sums = column_sums(matrix)
print(col_sums)  # 输出:[12, 15, 18]

这个示例中,我们使用了一个3x3的2D列表,并计算了每列的总和。在这个例子中,第一列的总和为12,第二列的总和为15,第三列的总和为18。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • Python数据分析(中英对照)·Using the NumPy Random Module 使用 NumPy 随机模块

    NumPy makes it possible to generate all kinds of random variables. NumPy使生成各种随机变量成为可能。 We’ll explore just a couple of them to get you familiar with the NumPy random module. 为了让您熟悉NumPy随机模块,我们将探索其中的几个模块。 The reason for using NumPy to deal with random variables is that first, it has a broad range of different kinds of random variables. 使用NumPy来处理随机变量的原因是,首先,它有广泛的不同种类的随机变量。 And second, it’s also very fast. 第二,速度也很快。 Let’s start with generating numbers from the standard uniform distribution,which is a the completely flat distribution between 0 and 1 such that any floating point number between these two endpoints is equally likely. 让我们从标准均匀分布开始生成数字,这是一个0和1之间完全平坦的分布,因此这两个端点之间的任何浮点数的可能性相等。 We will first important NumPy as np as usual. 我们会像往常一样,先做一个重要的事情。 To generate just one realization from this distribution,we’ll type np dot random dot random. 为了从这个分布生成一个实现,我们将键入np-dot-random-dot-random。 And this enables us to generate one realization from the 0 1 uniform distribution. 这使我们能够从01均匀分布生成一个实现。 We can use the same function to generate multiple realizations or an array of random numbers from the same distribution. 我们可以使用同一个函数从同一个分布生成多个实现或一个随机数数组。 If I wanted to generate a 1d array of numbers,I will simply insert the size of that array, say 5 in this case. 如果我想生成一个一维数字数组,我只需插入该数组的大小,在本例中为5。 And that would generate five random numbers drawn from the 0 1 uniform distribution. 这将从0-1均匀分布中产生五个随机数。 It’s also possible to use the same function to generate a 2d array of random numbers. 也可以使用相同的函数生成随机数的2d数组。 In this case, inside the parentheses we need to insert as a tuple the dimensions of that array. 在本例中,我们需要在括号内插入该数组的维度作为元组。 The first argument is the number of rows,and the second argument is the number of columns. 第一个参数是行数,第二个参数是列数。 In this case, we have generated a table — a 2d table of random numbers with five rows and three columns. 在本例中,我们生成了一个表——一个由五行三列随机数组成的二维表。 Let’s then look at the normal distribution. 让我们看看正态分布。 It requires the mean and the standard deviation as its input parameters. 它需

    01

    时序分析五边形战士!清华提出TimesNet:预测、填补、分类、检测全面领先|ICLR 2023

    ---- 新智元报道   编辑:LRS 好困 【新智元导读】时间序列分析在现实世界中的应用非常广泛,覆盖气象、工业、医疗等众多领域。近期,清华大学软件学院机器学习实验室提出了时序基础模型TimesNet,在长时、短时预测、缺失值填补、异常检测、分类五大任务上实现了全面领先。 实现任务通用是深度学习基础模型研究的核心问题,也是近期大模型方向的主要关注点之一。 然而,在时间序列领域,各类分析任务的差别较大,既有需要细粒度建模的预测任务,也有需要提取高层语义信息的分类任务。如何构建统一的深度基础模型高效地完

    02

    python list

    同属于一个列表的数据,可以是不同的类型 特色:存储于用一个列表的数据都是以数字来作为索引的,即作为操作存取其中各个元素的依据。 a_list 0 1 2 3 4 int int int int int 1 3 5 7 9 索引分别为 0,1,2,3,4 每个元素可有自已的类型,均为int,内容分别是 1、3、5、7、9 a_list = [ 1,3,5,7,9 ] 数字列表 \>>> a_list=[1,3,5,7,9] \>>> a_list [1, 3, 5, 7, 9] \>>> a_list[0] 1 字符串列表 \>>> str_list=['P','y','t','h','o','n'] \>>> str_list ['P', 'y', 't', 'h', 'o', 'n'] \>>> str_list[2] 't' 字符串split 方法 \>>> str_msg="I Love Pyton" \>>> b_list=str_msg.split() \>>> b_list ['I', 'Love', 'Pyton'] 一个英文句子拆成字母所组成的列表,用list() 函数, \>>> str_msg="I Love Pyton" \>>> c_list=list(str_msg) \>>> c_list ['I', ' ', 'L', 'o', 'v', 'e', ' ', 'P', 'y', 't', 'o', 'n'] \>>> 同一个列表中可以用不同的数据类型,列表中也可以有其他的列表 \>>> k1=['book',10] \>>> k2=['campus',15] \>>> k3=['cook',9] \>>> k4=['Python',26] \>>> keywords=[k1,k2,k3,k4] \>>> keywords [['book', 10], ['campus', 15], ['cook', 9], ['Python', 26]] \>>> keywords[2] ['cook', 9] \>>> keywords[2][0] 'cook' \>>> keywords[2][1] 9 \>>> 可以使用”+“运算把两个列表放在一起,还可以 检测某一个数据是否在列表之中 \>>> "Python" in k4 True \>>> k4 in keywords True \>>> ["Python",26] in keywords True \>>> keywords+k1+k2 [['book', 10], ['campus', 15], ['cook', 9], ['Python', 26], 'book', 10, 'campus', 15] \>>>

    03
    领券