cancel
Showing results for 
Search instead for 
Did you mean: 

Calling python experts

 
Highlighted
Md_Shaahbuddin
Occasional Contributor

Calling python experts

Hi Python experts,

 

I havent posted here for a while but I am hoping some good willed python expert can help with a list/sort problem.

 

I have a python input file with the following names/scores in;

 

seb 99
seb 75
stefan 100
dominik 80
stefan 89
seb 66
dominik 90
mum 45
mojo 99

 

I need to read this file into a list then sort by score (not name) and display names/scores. BUT if a name appears > 1 time (which is does) then only display the highest score for that name.

 

Any help much appreciated!

 

Thanks,

 

Stefan

Unix Manager.

Colt Telecom

London

 

5 REPLIES 5
RJHall
Frequent Advisor

Re: Calling python experts

Well it's not a terribly sophisticated approach, but this seems to work:

 

from operator import itemgetter

data = [('seb', 99), ('seb', 75), ('stefan', 100),
        ('dominik', 80), ('stefan', 89), ('seb', 66),
        ('dominik', 90), ('mum', 45), ('mojo', 99) ]

data.sort( key=itemgetter(1), reverse=True )
data.sort( key=itemgetter(0) )
newdata = []
lastname = ''
for (name, value) in data:
        if name != lastname:
                newdata.append( (name, value) )
        lastname = name
print newdata

 The ampersand-colon-semicolon is supposed to be a colon, but the editor keeps changing it.

Md_Shaahbuddin
Occasional Contributor

Re: Calling python experts

Thanks RJHall for the reply!

 

Unfortunately my list looks different to yours.

 

my input file looks like this;

seb 99
seb 75
stefan 100
dominik 80
stefan 89
seb 66
dominik 90
mum 45
mojo 99

 

When I print my list it looks different to yours, mine looks like this;

['seb 99', 'seb 75', 'stefan 100', 'dominik 80', 'stefan 89', 'seb 66', 'dominik 90', 'mum 45', 'mojo 99']

 

but your data list looks like this and so your code works;

[('seb', 99), ('seb', 75), ('stefan', 100), ('dominik', 80), ('stefan', 89), ('seb', 66), ('dominik', 90), ('mum', 45), ('mojo', 99)]

 

How can I read in my input file to a list like yours ?

or copy my list to one like yours ? then your code will work a treat.

 

Thanks again for your time.

 

RJHall
Frequent Advisor

Re: Calling python experts

Well, if you had your data in a file called 'data':

 

from operator import itemgetter

data = []
f = open('data', 'r')
for line in f:
        value = line.rstrip('\n').split(" ")
        data.append( ( value[0], value[1] ) )

data.sort( key=itemgetter(1), reverse=True )
data.sort( key=itemgetter(0) )
newdata = []
lastname = ''
for (name, value) in data:
        if name != lastname:
                newdata.append( (name, value) )
        lastname = name

for (name, value) in newdata:
        print "%s %s" % (name, value)

For a larger file, some way to combine the two sort steps would probably be more efficient.

Md_Shaahbuddin
Occasional Contributor

Re: Calling python experts

thats fantastic, it works a treat. thanks!

 

one last question.

 

when we declare a variable right at the top of our code. eg;

counter = 0

 

later in a function even though we pass it with function(counter) it does not update the global variable. Each time we call our function its gone back to 0.

 

how do we change our global variable in a function so that it stays permanently changed when it returns to the main loop or we call other functions ?

 

 

 

RJHall
Frequent Advisor

Re: Calling python experts

Just declare the variable 'global' within the function.

 

myvar = 0

def my func():
    global myvar
    myvar = 1

Normally I wouldn't recommend using globals though.