LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   python comparisons (https://www.linuxquestions.org/questions/programming-9/python-comparisons-4175579774/)

rblampain 05-14-2016 08:07 AM

python comparisons
 
Python 2.7
I am learning Python and I cannot understand why the code below gives
Code:

SyntaxError: invalid syntax
Code:

if c == "." or c == "," or c == ":" or c == ";" or c == "!" or c == "?" or c == "'" or c == '"'
I have tried all the possibilities I could think of with the same result (= or using single quotes or spaces and removing the test on any quote).
The problem I am trying to solve consists of 2 arrays of words obtained from splitting 2 strings of UTF-8 text and trying to find if any word in both arrays is terminated by punctuation mark that the split function did not remove. This punctuation mark must be removed to subsequently compare words from the 2 arrays. Here is the code I used:
Code:

array_words = line.split(u" ")# variable 'line' being a line of text with punctuation
for word in array_words:
c = word[-1:]# variable 'c' supposed to hold one character from the right of variable 'word'

Perhaps there is a better way to do that which I do not know.

Can anyone make suggestions?

Thank you for your help.

pan64 05-14-2016 08:13 AM

would be nice to show us your code, not only parts:
if should be ended with a :, which is now missing
the indentation of the last line c = word[-1:] is not ok.

kolargol 05-14-2016 09:46 AM

You forgot tu put the : at the end of the line

dugan 05-14-2016 11:42 AM

There's a better way to do that comparison, you know.

Code:

for word in array_words:
    c = word[-1]
    if c in ".,:;!?;\"":
        # do stuff

I'd also question whether you need the argument to split.

grail 05-14-2016 12:18 PM

Another idea, you could try the punct character class in a regex as your test.

rblampain 05-14-2016 10:19 PM

Thank you for the answers, the missing ":" was the problem.
dugan's solution is better than my code and helps to understand how Python works.


All times are GMT -5. The time now is 12:32 PM.