FW: [Tutor] Finding items in list of lists.
Doug.Shawhan@gecits.ge.com
Doug.Shawhan@gecits.ge.com
Mon Mar 17 15:34:03 2003
Yep. Turns out (surprise) that I wanted python to do my thinking for me. The
following seems to do what I want-
So sorry to be so confusing. On the bright side, you folks _really_ helped
me with list comprehensions! :-)
(I am posting this from outlook, so I hope the linewrap doesn't look to
bad...)
------------------------snip-------------------------
import string
import xreadlines
# Grab data from disk
f=open("\\tmp\\sample.txt","r")
rawData=[]
for line in xreadlines.xreadlines(f):
rawData.append(string.split(line,'\t'))
# Get rid of the top "row", since it contains no useful data by default
del rawData[0]
# We want to sort by the shared value which is in the tenth "column"
db = {}
gather = []
for lines in rawData:
parentItem = lines[9]
for line in rawData:
if line[9] == parentItem:
gather.append(line)
db[parentItem]=gather
gather = []
# Now we take the data that have been sorted by parentItem and further
sort them by
# what type of item they are. For example, if the line has both a
printer and a duplex unit
# therein, the printer and duplex are sorted out and given an entry of
their own. This
# enables the items to be uploaded into dam with no issues.
cookedData = {} # <-- new dictionary for the second sort.
for each in db.keys():
sortdb = {} # <-- new dictionary for the item sort
for item in db[each]:
sortdb[item[12]] = item
# filter out the Printer/Duplex combinations
if sortdb.has_key('DPLX') and sortdb.has_key('PRT'):
print '%s printer // duplexer match'%each
filtered=[sortdb['PRT'], sortdb['DPLX']]
signify = sortdb['PRT']
signify = signify[8]
cookedData[signify]=filtered
del sortdb['PRT']
del sortdb['DPLX']
# and the Laptop/Keyboard combinations
elif sortdb.has_key('KBD') and sortdb.has_key('LAP'):
print '%s laptop // keyboard match'%each
filtered=[sortdb['LAP'], sortdb['KBD']]
signify = sortdb['LAP']
signify = signify[8]
cookedData[signify]=filtered
del sortdb['LAP']
del sortdb['KBD']
# now sort out the leftover items (usually Cpu/Monitor
combinations)
else:
old_potato = [] # <--A type of leftover (I crack me up.)
for leftover in sortdb.keys():
old_potato.append(sortdb[leftover])
# and finally add the leftovers to the cookedData.
cookedData[item[8]]=old_potato
# Now we place the various data into a single long string suitable for DAM
to ingest
for item in cookedData.keys():
print item, cookedData[item]
--------------------snip-----------------------
Any suggestions for cleanup or concision are welcomed!
d