943,513 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 267
  • Python RSS
Jun 17th, 2009
0

sortig a list

Expand Post »
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]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ccandillo is offline Offline
16 posts
since May 2008
Jun 17th, 2009
0

Re: sortig a list

Use a dictionary, with port number as the key, pointing to a list of servers running on that port.
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,301 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: HTML table parse problem (beautiful soup)
Next Thread in Python Forum Timeline: Fixing AttributeError





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC