On 4/18/2010 6:53 PM, C M Caine wrote:
# Example data for forms and timetable: forms = ["P7", "P8", "P9", "P10", "P11", "S7", "S8", "S9", "S10", "S11", "IMA", "CAT", "FOR", "RLS", "EMPTY"]timetable = ['CAT', 'P10', 'P8', 'EMPTY', 'EMPTY', 'EMPTY', 'S10', 'S8', 'IMA', 'EMPTY', 'S7', 'S10', 'P9', 'EMPTY', 'EMPTY', 'EMPTY', 'S7', 'EMPTY', 'EMPTY', 'RLS', 'FOR', 'EMPTY', 'EMPTY', 'EMPTY', 'S9', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'S8', 'IMA', 'S11', 'P8', 'EMPTY', 'IMA', 'EMPTY', 'EMPTY', 'S11', 'S11', 'EMPTY', 'EMPTY', 'EMPTY', 'P7', 'S9', 'P11', 'P11', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'EMPTY', 'P9', 'EMPTY', 'EMPTY', 'P8', 'FOR', 'S10', 'S11', 'S7', 'P7', 'EMPTY', 'EMPTY', 'IMA', 'EMPTY', 'S9', 'P10', 'P11', 'CAT', 'S8', 'P9', 'RLS'] def analyseTimetable(): "Find number of and spaces between each form."
Consider using defaultdict in the collections module. The first time a key is referenced it is automatically added with a specified value.
import collections numDict = collections.defaultdict(0) spaceDict = collections.defaultdict(0)
adjustedSpaceDict = {}
If you use enumerate then you can replace timetable[i] with key
for i, key in enumerate(timetable): numDict[key] += 1
Something is wrong in the following if statement, as both paths execute the same code.
if spaceDict['1st'+key] == 0: spaceDict['nth'+key] = i else: spaceDict['nth'+key] = i for form in forms: adjustedSpaceDict[form] = spaceDict['nth'+form] - \ spaceDict['1st'+form] return (numDict, adjustedSpaceDict) # End example This function works fine, but I think that using range(len(timetable)) is clumsy. On the other hand, I need the indexes to track the number of spaces between instances of each form. Without using another loop (so not using list.index), is there a way of getting the index of the list entries?
-- Bob Gailer 919-636-4239 Chapel Hill NC _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor