2014-10-19 120 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 
相關問題