Numbering Lists created with a For Loop

This is the place for queries that don't fit in any of the other categories.

Numbering Lists created with a For Loop

Postby AJNS » Fri Apr 19, 2013 12:01 pm

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!
AJNS
 
Posts: 3
Joined: Fri Apr 19, 2013 11:59 am

Re: Numbering Lists created with a For Loop

Postby Yoriz » Fri Apr 19, 2013 12:09 pm

Hi,
Indentation in python code is important, to maintain indentation when you post code on the forum please use code tags.
please read the following link for more information on how to post your questions/code.
http://www.python-forum.org/viewtopic.php?f=6&t=145
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Numbering Lists created with a For Loop

Postby AJNS » Fri Apr 19, 2013 12:18 pm

Sorry! Here it is with the proper indents:

Code: Select all
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)
AJNS
 
Posts: 3
Joined: Fri Apr 19, 2013 11:59 am

Re: Numbering Lists created with a For Loop

Postby setrofim » Fri Apr 19, 2013 12:37 pm

Use append() rather than += when adding to your results list. You can then number the results using enumerate() (just like you are already doing with the input lines):
Code: Select all
import re

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

for i, line in enumerate(results):
    print 'line %s: %s' % (i, line)
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Numbering Lists created with a For Loop

Postby AJNS » Fri Apr 19, 2013 1:34 pm

That's great! Thanks for your help.

Do you know if it's possible to find out the index of the items in the lists that are created?
AJNS
 
Posts: 3
Joined: Fri Apr 19, 2013 11:59 am

Re: Numbering Lists created with a For Loop

Postby setrofim » Fri Apr 19, 2013 1:40 pm

AJNS wrote:Do you know if it's possible to find out the index of the items in the lists that are created?

You can use index() method of the lists to find the index of an item in the corresponding list (an error will be raised if the item is not in the list).
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 5 guests