
Steve Howell wrote:
--- Ron Adam <rrr@ronadam.com> wrote:
However, I do think there could be very useful uses for a standard sorting structure of some sort. That's the sorting as in mail sorters, or category sorters, that produce several streams of output instead of just one.
Would that be called a de-comprehension?
Here is some category-sorting code, FWIW, where every employee, Fred or not, gets a 50% raise, and employees are partitioned according to their Fredness.
It doesn't use a general iterator, so maybe I'm missing your point.
Since you aren't really creating two lists, the problem below doesn't really fit this particular solution. But maybe we can make it work from a different point of view. emps = { 'Fred Smith': 50.0, 'Fred Jones': 40.0, 'Joe Blow': 30, } def pay_increase(salary): return salary * 0.5 def is_fred(emp): return 'Fred' in emp[0] # give all Freds a raise freds, notfreds = decomp(emps.keys(), is_fred): for name in freds: emp[name] = pay_increas(emp[name]) # # Then we can use the freds list again to generate a report. # Of course in this case the following would work just as well... freds = [] for name in emps: if is_fred(name): emp[name] = pay_increas(emp[name]) freds.append(name) One reason to generating more than one list is if each list is going to be handled as batches, or in different ways, or at different times than you otherwise would by just iterating it. Cheers, Ron
def partitions(lst): dct = {} for k, value in lst: dct.setdefault(k, []).append(value) return dct.items()
def is_fred(emp): return 'Fred' in emp[0]
emps = [ ('Fred Smith', 50), ('Fred Jones', 40), ('Joe Blow', 30), ]
def pay_increase(salary): return salary * 0.5
emp_groups = partitions([(is_fred(emp), (emp[0], pay_increase(emp[1]))) for emp in emps])
for fredness, emps in emp_groups: print print 'is Fred?', fredness for name, pay_increase in emps: print name, pay_increase
----
is Fred? False Joe Blow 15.0
is Fred? True Fred Smith 25.0 Fred Jones 20.0
____________________________________________________________________________________ Park yourself in front of a world of choices in alternative vehicles. Visit the Yahoo! Auto Green Center. http://autos.yahoo.com/green_center/