LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2016, 08:07 AM   #1
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,289

Rep: Reputation: 52
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.
 
Old 05-14-2016, 08:13 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,936

Rep: Reputation: 7323Reputation: 7323Reputation: 7323Reputation: 7323Reputation: 7323Reputation: 7323Reputation: 7323Reputation: 7323Reputation: 7323Reputation: 7323Reputation: 7323
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.
 
1 members found this post helpful.
Old 05-14-2016, 09:46 AM   #3
kolargol
Member
 
Registered: Jan 2003
Posts: 56

Rep: Reputation: 3
You forgot tu put the : at the end of the line
 
1 members found this post helpful.
Old 05-14-2016, 11:42 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
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.

Last edited by dugan; 05-14-2016 at 11:46 AM.
 
2 members found this post helpful.
Old 05-14-2016, 12:18 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Another idea, you could try the punct character class in a regex as your test.
 
2 members found this post helpful.
Old 05-14-2016, 10:19 PM   #6
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,289

Original Poster
Rep: Reputation: 52
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Date comparisons ... muralimunna Linux - Newbie 3 05-03-2012 10:18 PM
two comparisons GerardM Linux - Newbie 6 06-15-2009 08:41 PM
string comparisons ricmon Programming 18 06-12-2007 10:31 AM
CPU comparisons lothario Linux - Hardware 2 12-11-2006 08:25 PM
Firewall Comparisons Please floppywhopper Linux - Security 11 10-02-2004 07:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration