Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:遍历列表中的列以查找回文_Python_List_Palindrome - Fatal编程技术网

Python:遍历列表中的列以查找回文

Python:遍历列表中的列以查找回文,python,list,palindrome,Python,List,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']] 我必须显示这个列表中的所有回文,这些回文既有行也有列。 我已经编码来查找行中的所有回文。但无法实现在列中查找回文的方法 以下是我目前的代码: result_1="" if len(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']]
我必须显示这个列表中的所有回文,这些回文既有行也有列。 我已经编码来查找行中的所有回文。但无法实现在列中查找回文的方法

以下是我目前的代码:

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
其中“转子”是回文

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

非常感谢您的帮助。提前谢谢

您可以使用转换列表:

>>> 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']
这将完成以下工作:

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)] 
这将修复列并迭代行。代码的其余部分与您的结构类似


我看到您不想使用Zip(我建议您使用):

备选答案: 您可以通过使用反向列表[:-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']]

有很多方法可以做到这一点由于您的努力,我将以您的代码为例

代码后面的另一种选择是在另一个列表中创建列,并检查其中是否有回文:

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)

这应该行得通。第一个循环遍历列表,第二个循环遍历每个列表

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

X范围内的i(0,透镜,1)的
:
str=“”
对于s中的j:
str=str+j[i]
打印str
如果str==str[:-1]:
打印str,“是一个苍白的罗马柱”,i
其他:
print str,“不是苍白的罗马柱”,i

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

def transpose(matrix):
    if not matrix:
        return []
    return [[row[i] for row in matrix] for i in range(len(matrix[0]))]
一旦使用转置修改输入,同样的逻辑也应该起作用


希望这有帮助

因为我是Python的初学者,所以我正在尝试用基本的代码编写代码,所以有没有其他方法不使用“zip”呢@Eric@eye是Python中的内置函数,我想说它属于“基础”Eric,非常出色,非常干净的解决方案!这是一个加号!太棒了,米里亚姆!我不这么认为,这是一个加号,你可以检查我的答案,看看另一个选择,如果你喜欢:)眼睛,让我知道如果你不明白我的答案,祝你好运!
def transpose(matrix):
    if not matrix:
        return []
    return [[row[i] for row in matrix] for i in range(len(matrix[0]))]