[Tutor] Searching a text file's contents and comparing them toalist

bob gailer bgailer at gmail.com
Thu Jul 15 03:53:45 CEST 2010


[snip]

Since you look up the items in the grocery list it seems to me a 
dictionary relating each item to its aisle would be best.

inventory = {"chips" : 1, "bread" : 1, "pretzels" : 1, "magazines" : 1,
              "juice" : 2, "ice cream" : 2,
              "asparagus" : 3}
MAX_AISLE = 3
aisles = [[] for i in range(MAX_AISLE)]
for item in grocery_list:
   aisle = inventory.get(item.strip(), MAX_AISLE)
   aisles[aisle-1].append(item)

To address the formatting question:

for aisleNo, items in enumerate(aisles):
   if aisleNo < MAX_AISLE - 1:
     print "Located on aisle %s" % aisleNo + 1
   else:
     print "Not in store"
   print "\n".join(items)


-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list