Keeping two lists aligned after processing

P

philly_bob

"""
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()
 
P

Paul Rubin

philly_bob said:
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.
 
P

philly_bob

philly_bob said:
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...46210e83b9?lnk=st&q=b0bm00r3#489f1746210e83b9

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

Bob=
 
T

Terry Reedy

| > 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top