Python: Iterating over columns in a list of list to find palindromes

Here I have a list of words like:

[['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]

And I have to display all the palindromes in this list that are in rows as well as in columns. I coded to find all palindromes in rows. But it cannot implement the method of searching palindromes in columns.

Here is my code:

result_1=""
if len(palindrome)==len_line_str:
    for row in range(len(palindrome)):
        for horizontal_line in range(len(palindrome[row])):
            if ''.join(palindrome[row])==''.join(reversed(palindrome[row])):
                result_1=''.join(palindrome[row])+" is a palindrome starting at ["+str(row)+"]["+str(row)+"] and is a row in the table"
    print(result_1)

The output will be displayed:

rotor is a palindrome starting at [0][0] and is a row in the table

Where the "rotor" is a palindrome.

I need a method to get palindromes in columns that are: "refer", "tenet", "radar"

Any help is greatly appreciated. Thanks in advance!

+4
source share
6 answers

You can use zipto transfer your lists:

>>> t = [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
[['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
>>> list(zip(*t))
[('r', 'e', 'f', 'e', 'r'), ('o', 'v', 'i', 'n', 'a'), ('t', 'e', 'n', 'e', 't'), ('o', 'i', 'e', 't', 'e'), ('r', 'a', 'd', 'a', 'r')]

, , . , :

>>> rows = [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
>>> [''.join(row) for row in rows if row[::-1] == row ]
['rotor']
>>> [''.join(column) for column in zip(*rows) if column[::-1] == column ]
['refer', 'tenet', 'radar']
+4

:

palindrome=[['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
n=len(palindrome)
for col in range(len(palindrome[0])):
    col_word=[palindrome[i][col] for i in range(n)]
    if ''.join(col_word)==''.join(reversed(col_word)):
        result=''.join(col_word)+" is a palindrome starting at ["+str(col)+"] and is a col in the table"
        print(result)

refer is a palindrome starting at [0] and is a col in the table
tenet is a palindrome starting at [2] and is a col in the table
radar is a palindrome starting at [4] and is a col in the table

, ,

col_word=[palindrome[i][col] for i in range(n)] 

. , .

+2

, Zip ( ):

:

list_ = [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]

(), [:: - 1]:

[i==i[::-1] for i in list_]
# prints [True, False, False, False, False]

() 1. ( list_2) 2. , :

list_2 = [[i[ind] for i in list_] for ind in range(len(list_))]
[i==i[::-1] for i in list_2]
# prints [True, False, True, False, True]

Update

, :

[i for i in list_ if i==i[::-1]] 
# prints [['r', 'o', 't', 'o', 'r']]
# and list_2: [['r', 'e', 'f', 'e', 'r'],['t', 'e', 'n', 'e', 't'],['r', 'a', 'd', 'a', 'r']]
+1

. -

, , , :

palindrome = [['r', 'o', 't', 'o', 'r'], 
              ['e', 'v', 'e', 'i', 'a'], 
              ['f', 'i', 'n', 'e', 'd'], 
              ['e', 'n', 'e', 't', 'a'], 
              ['r', 'a', 't', 'e', 'r']]

len_line_str = 5

result_1=""

def is_pal(string):
  return string == reversed(string)

colums = []
if len(palindrome)==len_line_str:
  for row in range(len(palindrome)):
    vertical = []
    if ''.join(palindrome[row])==''.join(reversed(palindrome[row])):
      result_1+=''.join(palindrome[row])+" is a palindrome starting at ["+str(0)+"]["+str(row)+"] and is a row in the table. " + "\n"
    for horizontal_line in range(len(palindrome[row])):
      if(len_line_str-1 > horizontal_line): 
        vertical += [palindrome[horizontal_line][row]]
      else:
        vertical += [palindrome[horizontal_line][row]]
        colums += [(vertical,row)]

  for word in colums:
    if ''.join(word[0])==''.join(reversed(word[0])):
        result_1+=''.join(word[0])+" is a palindrome starting at ["+str(0)+"]["+str(word[1])+"] and is a column in the table" + "\n"
  print(result_1)
+1

. s, .

, s - - [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', ​​'i ',' a '], [' f ',' ',' n ',' e ',' d '], [' e ',' n ',' e ',' t ',' a '], ['r', 'a', 't', 'e', ​​'r']]

for i in xrange(0,len(s),1):
    str = ""
    for j in s:
        str = str + j[i]
    print str
    if str == str[::-1]:
        print str," is a pallindrome - column", i
    else:
        print str," is not a pallindrome - column", i
0

There is no column traversal in Python. One hacky way you can follow is to perform a transpose operation on your input matrix. The following is a simple way to implement transposition using list methods.

def transpose(matrix):
    if not matrix:
        return []
    return [[row[i] for row in matrix] for i in range(len(matrix[0]))]

Your logic should work after changing your input using transposition.

Hope this helps!

0
source

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


All Articles