#!/usr/bin/env python
import sys
##Initials,Last,First,ShiftStart,ShiftEnd
s='''BAB,Smith,Bob,10:00a,1:00p
JCJ,Jones,Jill,11:00a,3:00p
JIH,Hernandez,Jose,1:00p,4:00p
BAB,Smith,Bob,1:00p,3:00p
JIH,Hernandez,Jose,5:00p,9:00p
JCJ,Jones,Jill,3:00p,3:30p
JCJ,Jones,Jill,3:30p,5:00p
DJM,Martin,Dominique,8:00a,11:00a'''
db = {}
for line in s.split('\n'):
Initials,Last,First,ShiftStart,ShiftEnd = line.split(',')
if Initials in db:
db[Initials][2].append((ShiftStart,ShiftEnd))
else:
db[Initials] = (Last,First,[(ShiftStart,ShiftEnd)])
for Initials,v in db.iteritems():
Last,First,shifts = v
sys.stdout.write(Initials + ',')
sys.stdout.write(Last + ',' + First)
for shift in shifts:
ShiftStart,ShiftEnd = shift
sys.stdout.write(',' + ShiftStart + ',' + ShiftEnd)
sys.stdout.write('\n')
或者,你可以做一个非常面向对象的程序:
import sys
##Initials,Last,First,ShiftStart,ShiftEnd
s='''BAB,Smith,Bob,10:00a,1:00p
JCJ,Jones,Jill,11:00a,3:00p
JIH,Hernandez,Jose,1:00p,4:00p
BAB,Smith,Bob,1:00p,3:00p
JIH,Hernandez,Jose,5:00p,9:00p
JCJ,Jones,Jill,3:00p,3:30p
JCJ,Jones,Jill,3:30p,5:00p
DJM,Martin,Dominique,8:00a,11:00a'''
class Shift(object):
def __init__(self,ShiftStart,ShiftEnd):
self.ShiftStart,self.ShiftEnd = ShiftStart,ShiftEnd
def __str__(self):
return '%s,%s' % (ShiftStart,ShiftEnd)
class Person(object):
def __eq__(self, p):
if self.Initials != p.Initials:
return False
if p.Last is not None and self.Last != p.Last:
return False
if p.First is not None and self.First != p.First:
return False
return True
def __init__(self,Initials,Last,First):
self.Initials,self.Last,self.First = Initials,Last,First
self.Shifts = []
def __str__(self):
return '%s,%s,%s' % (self.Initials,self.Last,self.First)
def AddShift(people, person, shift):
try:
person = people[people.index(person)]
except ValueError:
people.append(person)
person.Shifts.append(shift)
people = []
for line in s.split('\n'):
Initials,Last,First,ShiftStart,ShiftEnd = line.split(',')
AddShift(people, Person(Initials,Last,First), Shift(ShiftStart,ShiftEnd))
for person in people:
print '%s,%s' %(person, ','.join(map(str,person.Shifts)))