1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Rep Power

    Numbering Lists created with a For Loop

    I've created a program which outputs the capitals from different sentences within a file. So the output is like this:

    line 0: the dog_SUBJ bit_VERB the cat_OBJ

    ['SUBJ', 'VERB', 'OBJ']
    line 1: the man_SUBJ ran_VERB

    ['SUBJ', 'VERB']
    line 2: the cat_SUBJ ate_VERB the cheese_OBJ

    ['SUBJ', 'VERB', 'OBJ']

    I want to number the lists of 'SUBJ', 'VERB' and 'OBJ' for each sentence so I can compare the order. I would like to do it all in the same loop (if it is possible). Can anybody help me do this with my current script?

    Here is the code:

    import re
    with open('findallEX.txt', 'r') as f:
    for ii, line in enumerate(f):
    print 'line %s: %s' % (ii, line)
    results = []
    results += re.findall(r'[A-Z]+', line)

  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    39N 104.28W
    Rep Power
    change results += re.findall(r'[A-Z]+', line)
    to results.append([[ii]+re.findall(r'[A-Z]+', line)])
  4. #3
  5. Contributing User

    Join Date
    Aug 2011
    Rep Power
    As I see it, there are 16 classifications.
    UNFOUND = 666
    order = {}
    for POS in 'SUBJ VERB OBJ'.split():
        order[POS] = sentence.find(POS)
    for POS in 'SUBJ VERB OBJ'.split():
        if order[POS] < 0:
            order[POS] = UNFOUND
     0 None
     1 S      subject only
     2 V
     3 O
     4 SV
     5 VS  verb, subject, no object.
     6 SO
     7 OS
     8 VO
     9 OV
    10 SVO
    11 SOV
    12 VSO
    13 VOS
    14 OSV
    15 OVS
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo