Hello I hope this will be clearer


here is the script :

####################################

# -*- coding: utf-8 -*-
import csv

lines = csv.reader(open("CATEGORY.csv","r"))
lines2 = csv.reader(open("CATEGORYLIST.csv","r"))

old_cats = []
for line in lines:
    print line
    stories = []
    for line2 in lines2:
        print "\t%s == %s ? %s" % (line2[1], line[0], line2[1] == line[0])
        if line2[1] == line[0]:
            stories.append(line2[0])
    old_cats.append((line[0],line[2], stories))

####################################

here's the output :

['100701', '22', 'Water softeners', '2', '0', '100401']
21092 == 100701 ? False
100418 == 100701 ? False
100440 == 100701 ? False
100405 == 100701 ? False
100704 == 100701 ? False
21079 == 100701 ? False
21081 == 100701 ? False
21075 == 100701 ? False
21085 == 100701 ? False
21076 == 100701 ? False
21082 == 100701 ? False
21085 == 100701 ? False
21092 == 100701 ? False
100396 == 100701 ? False
100700 == 100701 ? False
100335 == 100701 ? False
100335 == 100701 ? False
100420 == 100701 ? False
100421 == 100701 ? False
100335 == 100701 ? False
100337 == 100701 ? False
['100702', '22', 'Garment processing', '2', '0', '100491']
['100703', '22', 'Fume hoods', '2', '0', '100441']
['100704', '22', 'Tank vent breathers', '2', '0', '100405']
['100705', '22', 'Absorbent paper', '2', '0', '100438']
['100706', '22', 'Packaging machinery & equipment', '2', '0', '100403']
['100707', '22', 'Copper', '2', '0', '100428']
['100708', '22', 'Silver', '2', '0', '100428']
['100709', '22', 'Clinical waste disposal', '2', '0', '100418']
['100710', '22', 'Air decontamination systems', '2', '0', '100342']
['100711', '22', 'Instrument cleaner-processors', '2', '0', '100438']
['100712', '22', 'Pathogen identification', '2', '0', '100404']
['100713', '22', 'Biocidal wipes', '2', '0', '100517']
['100714', '22', 'Medical devices', '2', '0', '100418']
['100715', '22', 'Testing', '2', '0', '100491']


when can see that the inner loop is only executed for the first item of the top-level loop...


I've attached the 2 CSV files.

Thanks

Attachment: CATEGORYLIST.csv
Description: Binary data

Attachment: CATEGORY.csv
Description: Binary data


Begin forwarded message:

From: W W <[email protected]>
Date: 29 January 2009 11:43:37 GMT
To: "emmanuel.delaborde" <[email protected]>
Subject: Re: [Tutor] Problem with nested for-in

On Thu, Jan 29, 2009 at 5:06 AM, emmanuel.delaborde <[email protected]> wrote:
Hello,

I have the following snippet :

lines = csv.reader(open("CATEGORY.csv","r"))
lines2 = csv.reader(open("CATEGORYLIST.csv","r"))

old_cats = []
for line in lines:
      print line 

   stories = []
   for line2 in lines2:
          print line2 

       if line2[1] == line[0]:
           stories.append(line2[0])
   old_cats.append((line[0],line[2], stories))


what happens is that

for the first elt of lines, the nested for in runs fine
but then never seem to run again while the top level loop iterates through the rest of lines

consequently stories accumulate nothing...

Can someone explain please ?

I would try adding the two print statements above to your code. It might give you an idea about what's going on. It would also be helpful to provide us with some of your original data and the data you get once you've run the program.

HTH,
Wayne



-----------------------------------------------------------------------------------------------

This e-mail (and any attachments) is confidential and may contain 
personal views which are not the views of Cimex Media Ltd and 
any affiliated companies, unless specifically stated. It is intended 
for the use of the individual or group to whom it is addressed. If 
you have received it in error, please delete it from your system, 
do not use, copy or disclose the information in any way nor act in 
reliance on it and please notify [email protected]

A company registered in England  Wales. Company Number 03765711
Registered Office : The Olde Bakehouse, 156 Watling Street East, Towcester,
Northants NN12 6DB

This email was scanned by Postini, the leading provider in Managed Email Security.

_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to