0

I am pretty sure that there is a simple answer to this but I am completely stuck!

I have a list of lists of various numbers of words, and I am trying to see whether I can find these words in a text file. So if the list of words I want to find is:

stem=[[u'travail',u'electr'],[u'la',u'vou']]

Then I am looping through each word of each "row" of the stem list, and trying to find that word in a text file. This code returns the index of the matched position of a word.

for row in stem: 
       for j,i in enumerate(row):
           y=[match.start() for match in re.finditer(re.escape(i), lowe)]
              print y

output:

[669, 2102, 5810]
   [1452, 2120, 5628]
   [1582, 2912, 3109, 5711]
   [605, 761, 882, 948, 1126, 1132, 1357, 1646, 1936, 2011, 2765, 3286, 3316, 3512, 3821, 3839, 3879, 4012, 4052,   4159, 4417, 4457, 4492, 4699, 4813, 4850, 4921, 4966, 4991, 4998, 5008, 5046, 5118, 5201, 5359, 5506, 5680]

How do I get an output so it is like

 [[[669, 2102, 5810], [1452, 2120, 5628]], [[1582, 2912, 3109, 5711], [605, 761, 882, 948, 1126, 1132, 1357, 1646, 1936, 2011, 2765, 3286, 3316, 3512, 3821, 3839, 3879, 4012, 4052, 4159, 4417, 4457, 4492, 4699, 4813, 4850, 4921, 4966, 4991, 4998, 5008, 5046, 5118, 5201, 5359, 5506, 5680]]]

So that the output for each row is in its own list?Thank you!!

2
  • Also, what is the point of your enumerate? I don't see you use j anywhere, so it seems like you should just be able to do for i in row: (and then I would probably change the variable name i to column or something a little more clear) Commented Jul 24, 2013 at 7:44
  • You must use a result list where you will append every sublist. Commented Jul 24, 2013 at 7:45

1 Answer 1

3

If I understand correctly, something like this should do it:

output = []
for row in stem: 
   current = []
   output.append(current)
   for j,i in enumerate(row):
       y=[match.start() for match in re.finditer(re.escape(i), lowe)]
       current.append(y)

print output
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.