如何在Python中遍历一列? [英] How to loop through a column in Python?
问题描述
我已经看到了有关此问题的答案,但没有人帮助我.有些人使用了 numpy ,有些人使用了其他有助于Python更简单的平台来回答问题.我不需要这些类型的东西,我希望使用简单的Python而不导入库或其他任何东西.
I have seen answers about this question but no one helped me. Some used numpy, and some people answered using other platforms that help Python to be simpler. I don't want these type of things, I want with the simple Python without importing libraries or anything more.
让我们说:我想做一个方法来检查2D数组中是否至少有一个列具有相同的值. 例如:
Let's say: I would want to do a method that checks if there's at least one column in the 2D array that the column has the same values. For example:
arr = [[2,0,3],[4,2,3],[1,0,3]]
将arr
发送到我的方法将返回True
,因为在第三列中每个术语中都有数字3.
Sending arr
to my method would return True
because in the third column there is in each term the number 3.
我将如何编写此方法?如何遍历2D数组中的每一列?
How would I write this method? How do I loop through each column in the 2D array?
推荐答案
遍历列
如何遍历2D数组中的每一列?
How do I loop through each column in the 2D array?
为了遍历每一列,只需遍历转置矩阵(转置矩阵只是一个新矩阵原来矩阵的行现在是列,反之亦然).
In order to loop through each column just loop through the transposed matrix (a transposed matrix is just a new matrix where the rows of original matrix are now columns and vice-versa).
# zip(*matrix) generates a transposed version of your matrix
for column in zip(*matrix):
do_something(column)
您提出的问题/示例的答案
我想做一个检查至少一列的方法 在二维数组中该列具有相同的值
I would want to do a method that checks if there's at least one column in the 2D array that the column has the same values
常规方法:
def check(matrix):
for column in zip(*matrix):
if column[1:] == column[:-1]:
return True
return False
单线:
arr = [[2,0,3],[4,2,3],[1,0,3]]
any([x[1:] == x[:-1] for x in zip(*arr)])
说明:
arr = [[2,0,3],[4,2,3],[1,0,3]]
# transpose the matrix
transposed = zip(*arr) # transposed = [(2, 4, 1), (0, 2, 0), (3, 3, 3)]
# x[1:] == x[:-1] is a trick.
# It checks if the subarrays {one of them by removing the first element (x[1:])
# and the other one by removing the last element (x[:-1])} are equals.
# They will be identical if all the elements are equal.
equals = [x[1:] == x[:-1] for x in transposed] # equals = [False, False, True]
# verify if at least one element of 'equals' is True
any(equals) # True
更新01
@BenC写道:
Update 01
@BenC wrote:
您也可以跳过列表理解中的[],以便任何 只是得到了可以提前停止一次/如果返回的发电机 假"
"You could also skip the [] around the list comprehension so that any just gets a generator that can be stopped early once/if it returns false"
如此:
arr = [[2,0,3],[4,2,3],[1,0,3]]
any(x[1:] == x[:-1] for x in zip(*arr))
更新02
您还可以使用集合(与@HelloV的答案合并).
Update 02
You could also use sets (merged with the answer of @HelloV).
单线:
arr = [[2,0,3],[4,2,3],[1,0,3]]
any(len(set(x))==1 for x in zip(*arr))
常规方法:
def check(matrix):
for column in zip(*matrix):
if len(set(column)) == 1:
return True
return False
集合没有重复的元素,因此,如果将列表转换为集合set(x)
,则任何重复的元素都会消失,因此,如果所有元素相等,则结果集的长度等于一个len(set(x))==1
.
A set does not have repeated elements, so if you transform a list into a set set(x)
any duplicated element goes away, so, if all elements are equals, the lenght of resulting set is equal to one len(set(x))==1
.
这篇关于如何在Python中遍历一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!