How to skip columns in Python?

I had seen the answers to this question, but no one helped me. Some used numpy, and some people responded using other platforms that help Python be simpler. I don't want this type of thing, I want with plain Python without importing libraries or anything else.

Let's say: I would like to make a method that checks for the presence of at least one column in a 2D array, that the column has the same values. For instance:

arr = [[2,0,3],[4,2,3],[1,0,3]] 

Sending arr to my method will return True , because the third column in each member contains the number 3.

How do I write this method? How to skip each column in a 2D array?

+5
source share
4 answers

Skip column

How do I scroll through each column in a 2D array?

To iterate over each column, simply swipe through the transposed matrix (the transposed matrix is ​​just a new matrix in which the rows of the 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) 

Response to the proposed problem / example

I would like to make a method that checks for the presence of at least one column in a 2D array, that the column has the same values

General method:

 def check(matrix): for column in zip(*matrix): if column[1:] == column[:-1]: return True return False 

Single line:

 arr = [[2,0,3],[4,2,3],[1,0,3]] any([x[1:] == x[:-1] for x in zip(*arr)]) 

Explanation:

 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 

Update 01

@BenC wrote:

"You can also skip [] around the list comprehension, so that anyone simply gets a generator that can be stopped earlier if it returns a lie"

So:

 arr = [[2,0,3],[4,2,3],[1,0,3]] any(x[1:] == x[:-1] for x in zip(*arr)) 

Update 02

You can also use sets (combined with @HelloV answer).

Single line:

 arr = [[2,0,3],[4,2,3],[1,0,3]] any(len(set(x))==1 for x in zip(*arr)) 

General method:

 def check(matrix): for column in zip(*matrix): if len(set(column)) == 1: return True return False 

The set has no duplicate elements, so if you convert the list to set(x) , any duplicate element will go away, so if all elements are equal, the length of the result set is equal to one len(set(x))==1 .

+6
source

A simple example, without adding the complexity of understanding lists and zip functions, is as follows:

 arr = [[2,0,3],[4,2,3],[1,0,2]] def check_column_equals_index(colum): for row in arr: if row[colum-1] != colum: return False return True print check_column_equals_index(3) 

which returns True if the 3rd column is 3 for each row.

However, since you may have read some other related discussions, it might be worth considering using Numpy or Pandas .

0
source
 1 in [len(set(i)) for i in zip(*arr)] 
0
source

Loop through a column in a 2D list and without import? How about retrieving the column elements in an auxiliary list, and then compare them with each other. You can control the process in this synoptic function:

 def checkcolumn(colnum, arrex=[]): for i in range(len(arr)): arrex.append(arr[i][colnum]) if arrex.count(arrex[0]) == len(arrex): return True else: return False print checkcolumn(1) 

It seems easier and prettier for me. Other ways to compare items in a column can be found at: https://www.csestack.org/python-check-if-all-elements-in-list-are-same/

0
source

Source: https://habr.com/ru/post/1238786/


All Articles