2014-10-19 86 views
3

我正在编写一个程序,检查用户输入的单词或句子是否是回文。这是目前为止的程序:删除另一个列表中的列表中的成员

def reverse(text): 
    a = text[::-1] 
    if a == text: 
     print "Yes, it's a palindrome." 
    else: 
     print "No, it's not a palindrome." 

string = str(raw_input("Enter word here:")).lower() 

reverse(string) 

但是,此代码不适用于句子。所以,我试图做这样的:

import string 

def reverse(text): 
    a = text[::-1] 
    if a == text: 
     print "Yes, it's a palindrome." 
    else: 
     print "No, it's not a palindrome." 

notstring = str(raw_input("Enter word here:")).lower() 

liststring = list(notstring) 

forbiddencharacters = string.punctuation + string.whitespace 

listcharacters = list(forbiddencharacters) 

newlist = liststring - listcharacters 

finalstring = "".join(newlist) 

reverse(finalstring) 

我的目标是把标点符号和空格到一个列表,然后减去这些字符添加到用户的输入,使得程序可以知道它是一个回文甚至如果字符串有标点符号和/或空格。但是,我不知道如何将列表中的元素减去另一个列表中的元素。我通过创建另一个等于用户输入减去字符的列表不起作用(我在Xubuntu终端仿真器中试过)。除此之外,当我运行程序出现此错误:

Traceback (most recent call last): 
    File "reverse.py", line 12, in <module> 
    forbiddencharacters = string.punctuation + string.whitespace 
AttributeError: 'str' object has no attribute 'punctuation' 

好了,所以我已经改变了变量的名字,我没有得到上面的错误。现在我仍然不知道如何减去列表中的元素。

由于我是初学者程序员,这可能对你来说很愚蠢。如果是这样的话,我很抱歉。如果有人能解决我遇到的两个问题中的一个或两个,我会非常感激。在此先感谢您的帮助。对不起,英文不好,文章太长:)

+1

不要使用字符串作为变量名,然后尝试使用string模块 – 2014-10-19 20:08:02

+0

你说得对,我不知道 – chilliefiber 2014-10-19 20:10:25

+1

不叫'STR()'从返回的值'raw_input()'它已经有'str'类型。 – jfs 2014-10-19 20:12:51

回答

4

由于palindromes有各种语法技巧(空格,逗号等),因此您应该添加一些过滤。

palindrome = "Rail at a liar" 

def is_palindrome(text): 
    text = text.lower()        #Avoid case issues 
    text = ''.join(ch for ch in text if ch.isalnum()) #Strips down everything but alphanumeric characters 
    return text == text[::-1] 

if is_palindrome(palindrome): 
    print "Yes, it's a palindrome." 
else: 
    print "No, it's not a palindrome." 
+0

您的比我的好! – user590028 2014-10-19 20:20:36

1

你可以通过分割短语并将它存储在列表中来实现。我将使用你的函数(但有更好的pythonic方法来做到这一点)。

def reverse(textList1): 
    textList2 = textList1[::-1] #or we can use reversed(textList1) 
    if textList2 == text: 
     print "Yes, it's a palindrome." 
    else: 
     print "No, it's not a palindrome." 

test1= "I am am I" 

You should split the phrase and store it in a list: 
test1List= test1.split(' ') 

reverse(test1List) 
1

您处于正确的轨道上,但您已将标识符string用于两个不同的目的。

既然你分配给这个变量名行:

string = str(raw_input("Enter word here:")).lower() 

现在可以不再访问从import string属性string.punctuationstring.whitespace,因为这个名字string不再绑定到模块,但到用户输入代替。

+0

好吧我改变了变量的名称,现在我没有得到那个错误。谢谢! – chilliefiber 2014-10-19 20:08:57

1

有所不同的方法来测试一个字符串是否是回文

def palindrome(s): 
    s = s.lower() 
    ln=len(s) 
    for n in xrange(ln/2): 
     if s[n] != s[(ln-n)-1]: 
      return False 
    return True 

print palindrome('Able was I ere I saw Elba') 

仅供参考 - 你需要调整这个,如果你喜欢脱光标点和空格(左的一个练习OP)

+0

感谢您的练习,我一定会尝试一下,因为我需要学习很多 – chilliefiber 2014-10-19 20:32:24

0

检查回文简单,

这适用于这两个词和句子。

import string 

def ispalindrome(input_str): 
    input_str = list(input_str) 
    forbidden = list(string.punctuation + string.whitespace) 

    for forbidden_char in forbidden:    # Remove all forbidden characters 
     while forbidden_char in input_str: 
      input_str.remove(forbidden_char) 
    return input_str == list(reversed(input_str)) # Checks if it is a palindrome 


input_str = raw_input().lower() # Avoid case issues 
print ispalindrome(input_str)  # Get input 
相关问题