擅长:python、mysql、java
<p>你应该考虑使用Python字典,或者dict。collections库中还有一个OrderedDict。在</p>
<p>例如:</p>
<pre><code> import collections
ips_ordered_by_port = collections.OrderedDict()
with open(sys.argv[1], 'rt') as f:
reader = csv.reader(f)
for row in reader:
row_parts = row.split(',')
ip = row_parts[0]
for row_part_idx in range(1, len(row_parts)):
port = row_parts[row_part_idx]
ip_ordered_by_port = ips_ordered_by_port.get(port)
if ip_ordered_by_port is None:
ips_ordered_by_port[port] = ip
else:
ip_ordered_by_port.update(ip)
</code></pre>
<p>现在您将有一个按端口(键)排序的dict,对于每个键,项(值)将是ip地址。要遍历每个密钥,请为该密钥创建一个文件,并将我留给您的所有IP地址写入。在</p>
<p>当然,为了提高可读性和可测试性,可以将上述代码重构为几个函数。在</p>