Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 使用while循环检查输入是否为回文时的索引器_Python_Loops_While Loop_Palindrome - Fatal编程技术网

Python 使用while循环检查输入是否为回文时的索引器

Python 使用while循环检查输入是否为回文时的索引器,python,loops,while-loop,palindrome,Python,Loops,While Loop,Palindrome,我想知道如何使用Python检查输入是否是带有while循环的回文 谢谢: 我试过这个 i = 0 n = len(msg_list) while i < n: palindrome = msg_list[i] if palindrome == msg_list[-1]: print("Palindrome? True") msg_list.pop(-1) else: print("Palindrome? False

我想知道如何使用Python检查输入是否是带有while循环的回文

谢谢:

我试过这个

i = 0
n = len(msg_list)

while i < n:
    palindrome = msg_list[i]
    if palindrome == msg_list[-1]:
        print("Palindrome? True")
        msg_list.pop(-1)
    else:
        print("Palindrome? False")
    i=i+1
i=0
n=len(消息列表)
而i

但是在最后我收到一条错误消息,列表索引超出了范围

您不需要迭代到最后,只需要迭代到中间字符。当反向计数时,将每个字符与相同索引处的字符进行比较:

s = "abcca"
length = len(s)
i = 0

while i < length / 2 + 1:
    if s[i] != s[-i - 1]:
        print "Not Palindrome"
        break
    i += 1
else:
    print "Palidrome"
哦,它变成了两行。

带一个while循环

import string

palin = 'a man, a plan, a canal, panama'

def testPalindrome(in_val):
    in_val = in_val.lower()
    left, right = 0, len(in_val) - 1
    while left < right:
        char_left, char_right = '#', '#'
        while char_left not in string.lowercase:
            char_left = in_val[left]
            left += 1
        while char_right not in string.lowercase:
            char_right = in_val[right]
            right -= 1
        if char_left != char_right:
            return False
    return True

print testPalindrome(palin)

使用
反转的简短解决方案

for c, cr in s, reversed(s):
    if c != cr:
        print("Palindrome? False")
        break
else:
    print("Palindrome? True")

另一种方法是使用
while
循环。一旦两个字符不匹配,while循环就会停止,因此这是非常有效的,但在Python中当然不是最好的方法

def palindrome(word):
   chars_fw = list(word)
   chars_bw = list(reversed(word))
   chars_num = len(word)
   is_palindrome = True
   while chars_num:
       if chars_fw[chars_num-1] != chars_bw[chars_num-1]:
           is_palindrome = False
           break
       chars_num -= 1

   return is_palindrome 

我想我会为仍在看这个问题的人添加另一个选择。它使用while循环,并且相当简洁(尽管如此,我仍然更喜欢
if word=word[::-1]
方法

def is_palindrome(word):    
    word = list(word.replace(' ', ''))  # remove spaces and convert to list
    # Check input   
    if len(word) == 1:
        return True
    elif len(word) == 0:
        return False
    # is it a palindrome....    
    while word[0] == word[-1]:
        word.pop(0)
        word.pop(-1)    
        if len(word) <= 1:
            return True
    return False
def是回文(word):
word=list(word.replace(“”,)#删除空格并转换为list
#检查输入
如果len(word)==1:
返回真值
elif len(字)==0:
返回错误
#这是回文吗。。。。
而字[0]==字[-1]:
word.pop(0)
word.pop(-1)
如果len(字)
在这里使用while循环是人为的,但它将消耗整个列表并执行测试


如果不需要while循环,可以这样做:
all(a==b表示a,b在zip中(word,reversed(word))

需要while循环吗?是的,需要while循环。显示整个堆栈跟踪。但是OP只需要
while
:(否则它可以更短。)
def palindrome(word):
   chars_fw = list(word)
   chars_bw = list(reversed(word))
   chars_num = len(word)
   is_palindrome = True
   while chars_num:
       if chars_fw[chars_num-1] != chars_bw[chars_num-1]:
           is_palindrome = False
           break
       chars_num -= 1

   return is_palindrome 
def is_palindrome(word):    
    word = list(word.replace(' ', ''))  # remove spaces and convert to list
    # Check input   
    if len(word) == 1:
        return True
    elif len(word) == 0:
        return False
    # is it a palindrome....    
    while word[0] == word[-1]:
        word.pop(0)
        word.pop(-1)    
        if len(word) <= 1:
            return True
    return False
word = "quiniuq"
pairs = zip(word,reversed(word))
a,b = next(pairs)
try:
    while a == b:
        a,b = next(pairs)
    return False # we got here before exhausting pairs
except StopIteration:
    return True # a == b was true for every pair