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

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    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)

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

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

    Join Date
    Aug 2011
    Posts
    4,893
    Rep Power
    481
    As I see it, there are 16 classifications.
    Code:
    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