<p>我正在分析包含昵称和主机名的日志。我希望最后得到一个数组,其中包含主机名和最近使用的昵称。在</p>
<p>我有以下代码,它只在主机名上创建一个列表:</p>
<pre><code>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.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(host)
print hostnames
# ['foo@google.com', 'bar@hotmail.com', 'hi@to.you']
</code></pre>
<p>我想最后能有一个大致如下的结果会很好:</p>
^{pr2}$
<p>我的问题是找出主机名是否出现在这样的列表中</p>
<pre><code>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])
</code></pre>
<p>有什么简单的解决方法吗,或者我应该尝试另一种方法吗?我总是可以有一个包含对象或结构的数组(如果python中有这样的东西的话),但是我更喜欢数组问题的解决方案。在</p>