- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Calling python experts
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2015 03:25 AM
05-05-2015 03:25 AM
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
- Tags:
- Python
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2015 11:18 AM - edited 05-05-2015 11:19 AM
05-05-2015 11:18 AM - edited 05-05-2015 11:19 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2015 01:10 PM
05-05-2015 01:10 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2015 02:38 PM
05-05-2015 02:38 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2015 03:18 PM
05-05-2015 03:18 PM
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2015 09:49 AM
05-07-2015 09:49 AM
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.