9 Replies - 1041 Views - Last Post: 22 September 2011 - 01:41 PM Rate Topic: -----

#1 labradorguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Compare variables in same line of text

Posted 22 September 2011 - 10:59 AM

Im trying to compare two words in the same line, and if the two words I'm looking for appear next to one another in the same order I input it the count goes up by one, but the code I have wrote isn't working.

import sys

if len(sys.argv) < 2:
    print "usage: python", sys.argv[0], "filename word1 word2 filename1 filename2 ..."
    sys.exit(1)

count = 0
wordone = sys.argv[2]
wordtwo = sys.argv[3]

for line in open(sys.argv[1]):
    for x in line.split():
        if ((x == wordone) and (x+1 == wordtwo)) :
            count = count + 1

print "The word-pair \"", wordone, wordtwo , "\" occurs " , count , " times in file \"", sys.argv[1], "\""




Thank you for the help

Is This A Good Question/Topic? 0
  • +

Replies To: Compare variables in same line of text

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7501
  • View blog
  • Posts: 15,544
  • Joined: 16-October 07

Re: Compare variables in same line of text

Posted 22 September 2011 - 11:03 AM

In your code, x is a string. x+1 will be the same string with a "1" tacked on the end. Rather than using a for each style loop, you want load the results of the split into a variable. The do a for loop from 0 to len-l of the list you got.

This post has been edited by baavgai: 22 September 2011 - 11:04 AM

Was This Post Helpful? 0
  • +
  • -

#3 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: Compare variables in same line of text

Posted 22 September 2011 - 11:05 AM

The superficial error with your code is that x is a word, and you are adding 1 to it on line 13. That doesn't work.

A typical method of doing this would be to analyze the line as a whole, rather than word by word:
phrase = wordone + " " + wordtwo
for line in open(sys.argv[1]):
  if phrase in line:
    count += 1



This of course does not work if you have multiple spaces in your line; in that case, a regular expression would fit the bill.
Was This Post Helpful? 1
  • +
  • -

#4 labradorguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Re: Compare variables in same line of text

Posted 22 September 2011 - 11:21 AM

Thank you very much, that is exactly what I needed
Was This Post Helpful? 0
  • +
  • -

#5 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: Compare variables in same line of text

Posted 22 September 2011 - 11:22 AM

You're welcome! Come back any time.
Was This Post Helpful? 0
  • +
  • -

#6 labradorguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Re: Compare variables in same line of text

Posted 22 September 2011 - 01:07 PM

import sys

if len(sys.argv) < 2:
    print "usage: python", sys.argv[0], "filename word1 word2 filename1 filename2 ..."
    sys.exit(1)

count = 0
counttwo = 0

diff = 1000000

wordone = sys.argv[2]
wordtwo = sys.argv[3]

phrase = wordone + " " + wordtwo

for line in open(sys.argv[1]):
        if phrase in line:
            count += 1

print "The word-pair \"", phrase , "\" occurs " , count , " times in file \"", sys.argv[1], "\""

for filename in sys.argv[4:]:
    counttwo = 0
    for line in open(filename):
            if phrase in line:
                counttwo += 1
    print ">>> The word-pair \"", phrase , "\" occurs " , counttwo , " times in file \"", filename, "\""
    
#find the closest match
    if abs(counttwo - count) < diff:
        diff = abs(counttwo - count)
        cfilename = filename

print "File \"", cfilename, "\" is most similar to file \"", sys.argv[1], "\""



This works fine for almost all cases but when I have the same two words in one column multiple times such as "blah monkey blah monkey blah monkey" and phrase = blah monkey it will only return it as 1 time because once it finds phrase in line it jumps to next line. I though by adding in

for phrase in line.split():



that it would work but it just makes everything wrong then
Was This Post Helpful? 0
  • +
  • -

#7 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: Compare variables in same line of text

Posted 22 September 2011 - 01:17 PM

I guess I misunderstood the problem.

In this case, you won't need to analyze lines:
data = open("file", "r")
count = data.count(wordone + " " + wordtwo)
print "file has", count, "occurrences of", wordone, wordtwo


Was This Post Helpful? 0
  • +
  • -

#8 labradorguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Re: Compare variables in same line of text

Posted 22 September 2011 - 01:31 PM

No I keep getting this error

Traceback (most recent call last):
File "tcomp2.py", line 26, in <module>
count = data.count(wordone + " " + wordtwo)
AttributeError: 'file' object has no attribute 'count
Was This Post Helpful? 0
  • +
  • -

#9 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: Compare variables in same line of text

Posted 22 September 2011 - 01:34 PM

Line 1 data = open("file", "r").read()
Was This Post Helpful? 0
  • +
  • -

#10 labradorguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Re: Compare variables in same line of text

Posted 22 September 2011 - 01:41 PM

You are a lifesaver thank you very much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1