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:
[,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
]

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]

Use a dictionary, with port number as the key, pointing to a list of servers running on that port.

allports = list(set(total.keys()))
lines = total.values()

data_dict = {}
for port in allports:
    for line in lines:
        this_port = line[1]
        ## add key to dictionary if it doesn't exist, 
        ## pointing to an empty list
        if this_port not in data_dict:  
            data_dict[this_port] = []

        data_dict[this_port].append(line[0])

keys = data_dict.keys()
keys.sort()
print keys
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.