4

I am parsing a log containing nicknames and hostnames. I want to end up with an array that contains the hostname and the latest used nickname.

I have the following code, which only creates a list over the hostnames:

hostnames = []

# while(parsing):
#    nick = nick_on_current_line
#    host = host_on_current_line 

if host in hostnames:
    # Hostname is already present.
    pass
else:
    # Hostname is not present
    hostnames.append(host)

print hostnames
# ['[email protected]', '[email protected]', '[email protected]']

I thought it would be nice to end up with something along the lines of the following:

# [['[email protected]', 'John'], ['[email protected]', 'Mary'], ['[email protected]', 'Joe']]

My problem is finding out if the hostname is present in such a list

hostnames = []

# while(parsing):
#    nick = nick_on_current_line
#    host = host_on_current_line   

if host in hostnames[0]: # This doesn't work.
    # Hostname is already present.
    # Somehow check if the nick stored together 
    # with the hostname is the latest one
else:
    # Hostname is not present
    hostnames.append([host, nick])

Are there any easy fix to this, or should I try a different approach? I could always have an array with objects or structs (if there is such a thing in python), but I would prefer a solution to my array problem.

3 Answers 3

4

Use a dictionary instead of a list. Use the hostname as the key and the username as the value.

Sign up to request clarification or add additional context in comments.

1 Comment

A dict will do all you need. Repeated updates for the same host will only keep the last value, and accessing dict.items() will give back the array of host->nick entries.
3

Just use a dictionary instead.

names = {}

while(parsing):
    nick = nick_on_current_line
    host = host_on_current_line   

    names[host] = nick

1 Comment

Wow. A lot easier than I expected. Thanks.
2
if host in zip(*hostnames)[0]:

or

if host in (x[0] for x in hostnames):

1 Comment

Testing for membership in a set or dict is far more scaleable than testing within a list, especially for so nicely hashable an item as a hostname string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.