【问题标题】:Python: Iterating through the columns in a list of list to find the palindromesPython:遍历列表中的列以查找回文
【发布时间】:2017-07-23 09:04:43
【问题描述】:

这里我有一个单词列表:

[['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']]

而且我必须显示此列表中的所有回文,它们在行和列中。 我已经编码以查找行中的所有回文。但无法实现在列中查找回文的方法。

到目前为止,这是我的代码:

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)

将显示输出:

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

其中“转子”是回文。

我需要一种方法来获取以下列中的回文: “参考”、“信条”、“雷达”

非常感谢任何帮助。提前致谢!

【问题讨论】:

  • 眼睛,如果你不明白我的回答,请告诉我,祝你好运!

标签: python list palindrome


【解决方案1】:

您可以使用zip 转置您的列表:

>>> 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']

【讨论】:

  • 有没有其他方法不使用'zip',因为我正在尝试使用基础进行编码,因为我是 Python 的初学者。 @埃里克
  • @eye zip 是 Python 的内置函数,我说它属于“基础”
  • 埃里克,非常棒,非常干净的解决方案!这是一个加号!
【解决方案2】:

这样就可以了:

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)] 

这会修复列并遍历行。其余代码的结构与您的类似。

【讨论】:

  • 这太棒了米里亚姆!我没想到,这是一个加分项,如果您愿意,可以查看我的答案以查看另一种选择:)
【解决方案3】:

我看到你不想使用 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]

更新

如果你想直接得到答案,你可以这样做:

[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']]

【讨论】:

    【解决方案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']]
    
    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)
    

    【讨论】:

      【解决方案5】:

      这应该可以。第一个循环遍历列表,第二个循环遍历每个列表。

      假设 s 是列表的名称 - [['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']]

      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
      

      【讨论】:

        【解决方案6】:

        Python 中没有逐列遍历。您可以遵循的一种 hacky 方法是对输入矩阵执行转置操作。下面是使用列表推导实现转置的简单方法。

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

        使用转置修改输入后,您的相同逻辑应该可以工作。

        希望这会有所帮助!!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-22
          • 1970-01-01
          • 2017-04-22
          • 1970-01-01
          • 1970-01-01
          • 2016-08-27
          • 2011-09-14
          • 2022-11-22
          相关资源
          最近更新 更多