Removing members of a list in another list

I am writing a program that checks if a user-defined word or sentence is a palindrome or not. This is the program so far:

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

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

reverse(string)

      

However, this code doesn't work for suggestions. So I tried to do it like this:

import string

def reverse(text):
    a = text[::-1]
    if a == text:
        print "Yes, it a palindrome."
    else:
        print "No, it 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)

      

My goal is to put punctuation and space in the list and then subtract those characters into user input so that the program can determine if it is a palindrome even if the string has punctuation and / or spaces. However, I don't know how I can subtract items in a list to items in another list. The way I did it by creating another list that is equal to user input minus characters does not work (I tried it in Xubuntu terminal emulator). Also, when I run the program this error appears:

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

      

So, I changed the name of the variable and I am not wrong above. Now I still don't know how to subtract the elements of the lists.

Since I'm a beginner programmer, this may sound silly to you. If this is the case, sorry in advance. If anyone can solve one or both of the two problems I have, I would be extremely grateful. Thanks in advance for your help. Sorry for bad english and long post :)

+3


source to share


5 answers


You have to add some filtering along the path as palindromes have various syntactic tricks (spaces, commas, etc.).



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 a palindrome."
else:
    print "No, it not a palindrome."

      

+4


source


You can do this by splitting the phrase and saving it to the list. I'm going to use your function (but there are better pythonic ways to do this).



def reverse(textList1):
    textList2 = textList1[::-1]  #or we can use reversed(textList1)
    if textList2 == text:
        print "Yes, it a palindrome."
    else:
        print "No, it 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


source


You are on the right track, but you used the identifier string

for two different purposes.

Since you assigned the string to this variable name:

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

      

Now you can no longer access attributes to string.punctuation

and string.whitespace

from import string

, because the name is string

no longer tied to the module, but instead of user input.

+1


source


A slightly different approach to testing if the string is a palindrome

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

      

FYI - you need to tweak it to remove punctuation and space if you like (left an exercise for the OP)

+1


source


Palindrome checking is easy,

This works for both words and sentences.

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

      

0


source







All Articles