sortig a list

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2008
Posts: 11
Reputation: ccandillo is an unknown quantity at this point 
Solved Threads: 0
ccandillo ccandillo is offline Offline
Newbie Poster

sortig a list

 
0
  #1
Jun 17th, 2009
I cannot seem to figure this out. This part of a larger script that I am writing. I have a list that looks like this. The fields are servername, port and program. How do I sort it to get a tally of what servers are listening on what ports.

This is my list:
[['server1', '1045', 'winlogon.exe'],
['server1', '8001', 'ctxsgsvc.exe'],
['server1', '3704', 'winlogon.exe'],
['server1', '1043', 'snmp.exe'],
['server2', '1041', 'snmp.exe'],
['server2', '1040', 'bpjava-msvc.exe'],
['server3', '2226', 'winlogon.exe'],
['server4', '1045', 'winlogon.exe'],
['server4', '1049', 'svchost.exe'],
['server5', '1048', 'clussvc.exe'],
['server5', '4660', 'winlogon.exe'],
['server5', '4911', 'winlogon.exe'],
['server6', '2226', 'winlogon.exe'],
['server6', '1045', 'winlogon.exe'],
['server6', '4998', 'unsecapp.exe'],
['server7', '4001', 'winlogon.exe']]

This is what I want:

1045 is open on server1, server4
8001 is open on server1
2226 is open on server3, server6
etc...

So far this is what I have but it doen't quite work:
allports = list(set(total.keys()))
lines = total.values()

for port in allports:
    for line in lines:
        if line[1] == port:
            data[port] = line[0]
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,028
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 289
woooee woooee is offline Offline
Veteran Poster

Re: sortig a list

 
0
  #2
Jun 17th, 2009
Use a dictionary, with port number as the key, pointing to a list of servers running on that port.
  1. allports = list(set(total.keys()))
  2. lines = total.values()
  3.  
  4. data_dict = {}
  5. for port in allports:
  6. for line in lines:
  7. this_port = line[1]
  8. ## add key to dictionary if it doesn't exist,
  9. ## pointing to an empty list
  10. if this_port not in data_dict:
  11. data_dict[this_port] = []
  12.  
  13. data_dict[this_port].append(line[0])
  14.  
  15. keys = data_dict.keys()
  16. keys.sort()
  17. print keys
Linux counter #99383
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC