獲取 Python 中矩陣的第 N 列
當需要獲取矩陣的第“n”列時,可以使用“any”方法。
以下是對其進行演示 −
示例
my_list = [[34, 67, 89], [16, 27, 86], [48, 30, 0]] print("The list is : ") 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)
輸出
The list is : [[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
說明
定義一個 list 的 list,並在控制檯上顯示。
初始化 N 的值。
已在控制檯上顯示此值。
為 elem 變數分配一個整數值。
使用 any 方法檢視列表中是否有任何元素與前面定義的 elem 變數匹配。
結果儲存在變數中。
該結果將作為輸出顯示在控制檯上。
廣告