【发布时间】:2011-02-14 15:21:50
【问题描述】:
我正在解析包含昵称和主机名的日志。我想得到一个包含主机名和最新使用的昵称的数组。
我有以下代码,它只在主机名上创建一个列表:
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
# ['foo@google.com', 'bar@hotmail.com', 'hi@to.you']
我认为以如下方式结束会很好:
# [['foo@google.com', 'John'], ['bar@hotmail.com', 'Mary'], ['hi@to.you', 'Joe']]
我的问题是找出主机名是否存在于这样的列表中
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])
是否有任何简单的解决方法,或者我应该尝试不同的方法?我总是可以有一个包含对象或结构的数组(如果 python 中有这样的东西),但我更喜欢我的数组问题的解决方案。
【问题讨论】:
标签: python arrays multidimensional-array