Data distribution by frequency (one line algorithm)

Chris Akre dont at spam.me
Tue Oct 10 01:51:22 EDT 2000


Enjoy.

>>> events = {
    'obj1': 3,
    'obj2': 2,
    'obj3': 1,
    'obj4': 2 }

>>> list=reduce(lambda x,y: x+y, map(lambda t,q:map(lambda
x,t=1.*t/q[1],o=q[0]:((x-.5)*t,o),range(1,q[1]+1)),(reduce(lambda
x,y:x+y,events.values()),)*len(events),events.items()),[]);list.sort();map(l
ambda x:x[1],list)

['obj1', 'obj2', 'obj4', 'obj1', 'obj3', 'obj2', 'obj4', 'obj1']

>>> events = {
    'obj1':  5,
    'obj2':  9,
    'obj3': 18,
    'obj4':  2}

>>> list=reduce(lambda x,y: x+y, map(lambda t,q:map(lambda
x,t=1.*t/q[1],o=q[0]:((x-.5)*t,o),range(1,q[1]+1)),(reduce(lambda
x,y:x+y,events.values()),)*len(events),events.items()),[]);list.sort();map(l
ambda x:x[1],list)

['obj3', 'obj2', 'obj3', 'obj1', 'obj3', 'obj2', 'obj3', 'obj3', 'obj4',
'obj2', 'obj1', 'obj3', 'obj3', 'obj2', 'obj3', 'obj3', 'obj1', 'obj2',
'obj3', 'obj3', 'obj2', 'obj3', 'obj3', 'obj1', 'obj2', 'obj3', 'obj4',
'obj3', 'obj2', 'obj3', 'obj1', 'obj3', 'obj2', 'obj3']

Who says you can't write obfusicated code in Python-ly yours.

class sig:
    author = 'Chris Akre'
    email  = reduce(lambda x,y: x + y*(not y in 'SPAM'),
                    'cpSa at mPail.nAova.oMrg', '')



"Greg Fortune" <gfortune_ewu at ispchannel.com> wrote in message
news:WAOD5.14689$la6.197754 at news1.onlynews.com...
>
> I have several objects that I need to arrange in a list based on their
> respective frequency.  I'm basically trying to set up an intelligent user
> modifiable timing loop.
>
> Obj1
> Frequency: 2
> Obj2
> Frequency:2
>
> should yeild
> [Obj1, Obj2, Obj1, Obj2]
>
>
> Something a little nastier like
>
> Obj1
> Frequency: 3
> Obj2
> Frequency: 2
> Obj3
> Frequency: 1
> Obj4
> Frequency: 2
>
> Should yield
> [Obj1, Obj2, Obj4, Obj1, Obj3, Obj2, Obj1, Obj4]
> or something very similar.
>
> The only idea I have on the matter is take the most frequently occuring
> object and distribute it through the list at the appropriate intervals
(i.e.
> total_size/obj1_freq).  Then, create a temp list of the available elements
> and do the same thing with the next most frequently occuring object and
the
> new list.
>
> Sound good? Or is there an easier/cleaner way to accomplish the same
thing?
>
> Anyone know of any resources I can look at for information?
Implementation
> language doesn't really matter either.
>
>
> Greg Fortune
> Eastern Washington University Student
>
> Why is it, that coding C++ just isn't quite as cool now as it was before I
> learned Python? ;o) - Me
>
>
>





More information about the Python-list mailing list