在 Python 中获取矩阵的第 N 列
当需要获取矩阵的第'n'列时,可以使用'any'方法。
以下是相同的演示-
示例
my_list = [[34, 67, 89], [16, 27, 86], [48, 30, 0]] print("名单是: ") print(my_list) N = 1 print("The value of N has been initialized to -") print(N) elem = 30 my_result = any(sub[N] == elem for sub in my_list) print("Does the element exist in a particular column ? ") print(my_result)输出结果
名单是: [[34, 67, 89], [16, 27, 86], [48, 30, 0]] The value of N has been initialized to - 1 Does the element exist in a particular column ? True
解释
定义了一个列表列表,并显示在控制台上。
N的值被初始化。
这显示在控制台上。
elem变量被分配一个整数值。
any方法用于查看列表中的任何元素是否与之前定义的elem变量匹配。
结果存储在一个变量中。
这在控制台上显示为输出。