Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Keeping two lists aligned after processing

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


b0bm00r3 at gmail

May 10, 2008, 11:19 PM

Post #1 of 6 (67 views)
Permalink
Keeping two lists aligned after processing

"""
I have a population of five algorithms. Each Alg has a method,
Alg.accuracy(), which calculates its accuracy. Running the accuracy
method on each Alg, I end up with a list of accuracies like [0.75,
0.10, 0.45, 0.80, 0.45]

Now I want to store the algorithms and associated accuracies in a
file, in descending order of accuracy.

Here's how I do it now. There must be a better way! This method is
inelegant, and does not handle at all the issue of what to do with
duplicate accuracies, such as the two different algorithms with 0.45
accuracies in the example above. It merely saves the first of the
duplicates -- not what I want at all.

I want to end up with a file:

0 AlgD 0.80
1 AlgA 0.75
2 AlgC 0.45
3 AlgE 0.45
4 AlgB 0.10

"""

algs=['AlgA', 'AlgB', 'AlgC', 'AlgD', 'AlgE']
accs=[]
for alg in algs:
thisacc=getattr(alg.accuracy)() # picked this technique on
comp.lang.python last month
accs.append(thisacc)

unsortedaccs=[x for x in accs] # to preserve unsorted
accs.sort()
accs.reverse() # ending up with sorted list

nuorder=[]
for ax,acc in enumerate(accs):
spot=unsortedaccs.index(acc)
nuorder.append(spot)

ofn=open('store.alg','w')
for ord in nuorder:
alg=algs[ord]
acc=unsortedlist[ord]
ofn.write("%d %s,%s" % (ord,alg,str(acc))
ofn.close()

--
http://mail.python.org/mailman/listinfo/python-list


b0bm00r3 at gmail

May 10, 2008, 11:22 PM

Post #2 of 6 (64 views)
Permalink
Keeping two lists aligned after processing [In reply to]

"""
I have a population of five algorithms. Each Alg has a method,
Alg.accuracy(), which calculates its accuracy. Running the accuracy
method on each Alg, I end up with a list of accuracies like [0.75,
0.10, 0.45, 0.80, 0.45]

Now I want to store the algorithms and associated accuracies in a
file, in descending order of accuracy.

Here's how I do it now. There must be a better way! This method is
inelegant, and does not handle at all the issue of what to do with
duplicate accuracies, such as the two different algorithms with 0.45
accuracies in the example above. It merely saves the first of the
duplicates -- not what I want at all.

I want to end up with a file:

0 AlgD 0.80
1 AlgA 0.75
2 AlgC 0.45
3 AlgE 0.45
4 AlgB 0.10

"""

algs=['AlgA', 'AlgB', 'AlgC', 'AlgD', 'AlgE']
accs=[]
for alg in algs:
thisacc=getattr(alg.accuracy)() #comp.lang.python April
accs.append(thisacc)

unsortedaccs=[x for x in accs] # to preserve unsorted
accs.sort()
accs.reverse() # ending up with sorted list

nuorder=[]
for ax,acc in enumerate(accs):
spot=unsortedaccs.index(acc)
nuorder.append(spot)

ofn=open('store.alg','w')
for ord in nuorder:
alg=algs[ord]
acc=unsortedlist[ord]
ofn.write("%d %s,%s" % (ord,alg,str(acc))
ofn.close()
--
http://mail.python.org/mailman/listinfo/python-list


"http://phr.cx" at NOSPAM

May 10, 2008, 11:32 PM

Post #3 of 6 (64 views)
Permalink
Re: Keeping two lists aligned after processing [In reply to]

philly_bob <b0bm00r3[at]gmail.com> writes:
> algs=['AlgA', 'AlgB', 'AlgC', 'AlgD', 'AlgE']
> accs=[]
> for alg in algs:
> thisacc=getattr(alg.accuracy)() # picked this technique on
> comp.lang.python last month
> accs.append(thisacc)

I think what you mean is (untested):

algs = [AlgA, AlgB, AlgC, AlgD, AlgE]
accs = sorted(((alg.accuracy(), alg.name()) for alg in algs),
reverse=True)
for i,(alg_acc, alg_name) in enumerate(accs):
print '%2d %5s %0.2f'% (i, alg_name, alg_acc)

This assumes a method alg.name() which tells you the name of the
algorithm. I don't understand how you're converting the string 'AlgA'
to an algorithm object in your example above.
--
http://mail.python.org/mailman/listinfo/python-list


danb_83 at yahoo

May 10, 2008, 11:35 PM

Post #4 of 6 (64 views)
Permalink
Re: Keeping two lists aligned after processing [In reply to]

On May 11, 1:22 am, philly_bob <b0bm0...@gmail.com> wrote:
>
> I have a population of five algorithms. Each Alg has a method,
> Alg.accuracy(), which calculates its accuracy. Running the accuracy
> method on each Alg, I end up with a list of accuracies like [0.75,
> 0.10, 0.45, 0.80, 0.45]
>
> Now I want to store the algorithms and associated accuracies in a
> file, in descending order of accuracy.
>
> Here's how I do it now. There must be a better way! This method is
> inelegant, and does not handle at all the issue of what to do with
> duplicate accuracies, such as the two different algorithms with 0.45
> accuracies in the example above. It merely saves the first of the
> duplicates -- not what I want at all.
>
> I want to end up with a file:
>
> 0 AlgD 0.80
> 1 AlgA 0.75
> 2 AlgC 0.45
> 3 AlgE 0.45
> 4 AlgB 0.10

algs.sort(key=Alg.accuracy, reverse=True)
--
http://mail.python.org/mailman/listinfo/python-list


b0bm00r3 at gmail

May 10, 2008, 11:45 PM

Post #5 of 6 (64 views)
Permalink
Re: Keeping two lists aligned after processing [In reply to]

On May 11, 2:32 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> philly_bob <b0bm0...@gmail.com> writes:
> > algs=['AlgA', 'AlgB', 'AlgC', 'AlgD', 'AlgE']
> > accs=[]
> > for alg in algs:
> > thisacc=getattr(alg.accuracy)() # picked this technique on
> > comp.lang.python last month
> > accs.append(thisacc)
>
> I think what you mean is (untested):
>
> algs = [AlgA, AlgB, AlgC, AlgD, AlgE]
> accs = sorted(((alg.accuracy(), alg.name()) for alg in algs),
> reverse=True)
> for i,(alg_acc, alg_name) in enumerate(accs):
> print '%2d %5s %0.2f'% (i, alg_name, alg_acc)
>
> This assumes a method alg.name() which tells you the name of the
> algorithm. I don't understand how you're converting the string 'AlgA'
> to an algorithm object in your example above.

The technique is described in this thread:

http://groups.google.com/group/comp.lang.python/browse_frm/thread/ed652ba2fab47a6f/489f1746210e83b9?lnk=st&q=b0bm00r3#489f1746210e83b9

I've barely mastered it, but it does work.

Bob=
--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

May 11, 2008, 1:38 PM

Post #6 of 6 (53 views)
Permalink
Re: Keeping two lists aligned after processing [In reply to]

"Paul Rubin" <"http://phr.cx"@NOSPAM.invalid> wrote in message
news:7xbq3dfj0c.fsf[at]ruckus.brouhaha.com...
| philly_bob <b0bm00r3[at]gmail.com> writes:
| > algs=['AlgA', 'AlgB', 'AlgC', 'AlgD', 'AlgE']
| > accs=[]
| > for alg in algs:
| > thisacc=getattr(alg.accuracy)()
|> # picked this technique on comp.lang.python last month

I don't believe that you actually ran this.
What you should have picked up for the above was something like
globals()[alg].accuracy(). What you probably saw was something
like getattr(ob_with_call_attr, 'callable_name')().
But a list of alg objects instead of names, as Paul suggested,
is almost always better.

| > accs.append(thisacc)

| I think what you mean is (untested):
|
| algs = [AlgA, AlgB, AlgC, AlgD, AlgE]
| accs = sorted(((alg.accuracy(), alg.name()) for alg in algs),
reverse=True)

Use alg.__name__ instead of alg.name().

| for i,(alg_acc, alg_name) in enumerate(accs):
| print '%2d %5s %0.2f'% (i, alg_name, alg_acc)
|
| This assumes a method alg.name() which tells you the name of the
algorithm.

See above. In 3.0, function names are also .__name__ instead of
.func_name,
so one can mix callables in a list and get their definitions names
uniformly.

| I don't understand how you're converting the string 'AlgA'
| to an algorithm object in your example above.

He was trying to convert name to method with the buggy getattr call.

tjr




--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.