python - Remove members of a list in another list -



python - Remove members of a list in another list -

i'm writing programme checks if word or sentence given user input palindrome or not. programme far:

def reverse(text): = text[::-1] if == text: print "yes, it's palindrome." else: print "no, it's not palindrome." string = str(raw_input("enter word here:")).lower() reverse(string)

however, code doesn't work sentences. tried this:

import string def reverse(text): = text[::-1] if == text: print "yes, it's palindrome." else: print "no, it's not 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 set punctuation , whitespace list , subtracting characters input of user programme can tell if it's palindrome if string has punctuation and/or whitespace. however, don't know how can subtract elements in list elements in list. way did it, creating list equals user input minus characters doesn't work (i tried in xubuntu terminal emulator). apart that, when run programme error appears:

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

ok have changed variable name , don't error above. still don't know how subtract elements of lists.

since i'm beginner programmer might seem stupid you. if that's case, i'm sorry in advance. if can solve 1 or both of 2 problems have, i'd extremely grateful. in advance help. sorry bad english language , long post :)

you should add together filtering along way since palindromes have various syntax tricks (spaces, commas, etc.).

palindrome = "rail @ liar" def is_palindrome(text): text = text.lower() #avoid case issues text = ''.join(ch ch in text if ch.isalnum()) #strips downwards alphanumeric characters homecoming text == text[::-1] if is_palindrome(palindrome): print "yes, it's palindrome." else: print "no, it's not palindrome."

python python-2.7

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -