Python 中的嵌套列表或二维数组
Nested list or two dimensional array in Python
我有一个 IP 地址列表。我需要将这些 IP 地址存储在列表或数组中(不允许重复)并存储它们的数量。
例如我有
IP-1
IP-4
IP-7
IP-4
IP-2
IP-4
IP-2
此列表来自 .txt 文件。我有多个这样的文件,所以我不能像这样在代码中放置 list = [ [a,b], [c,d], [e,f] ]
(我的意思是不要在代码中静态地这样做,因为对于每个 .txt 文件,列表都会不同)。
所以在这个列表中我有:
IP-1 1x
IP-4 3x
IP-7 1x
IP-2 2x
我必须以某种方式将其全部存储在一个列表或数组中。例如像这样:
list_of_ips_and_their_counts = [ [IP-1,1], [IP-4,3], [IP-7,1], [IP-9,2] ]
现在我必须在这个list/array中搜索出现次数最多的IP并打印出来。例如:
print("IP address " + the_ip + " occured " + number + " times.") # IP address IP-4 occured 3 times.
我不确定如何存储 IP 地址及其计数。
您可以使用 collections.Counter
来计算文件中的每个 IP-{number}
行:
from collections import Counter
with open("test.txt") as f:
ip_counts = Counter(line.strip() for line in f)
# Counter({'IP-4': 3, 'IP-2': 2, 'IP-1': 1, 'IP-7': 1})
for ip_address, count in ip_counts.items():
print("IP address %s occured %d times" % (ip_address, count))
输出:
IP address IP-1 occured 1 times
IP address IP-4 occured 3 times
IP address IP-7 occured 1 times
IP address IP-2 occured 2 times
如果您愿意,也可以使用 map()
来计算行数:
ip_counts = Counter(map(str.strip, f))
注:str.strip()
这里用来去掉key的空格,比如把'IP-1\n'
转成IP-1
。由于您不需要包含空格,因此将来可以更轻松地访问密钥。
如果你想要最大计数,我会使用 max()
with operator.itemgetter()
:
print(max(ip_counts.items(), key=itemgetter(1)))
# ('IP-4', 3)
其中 returns 使用索引 1
处的计数的最大元组。
您可以使用 np.argmax()
来完成。那是你问题的第二步。但是最好每个线程问一个问题。
import numpy as np
result = ip[np.argmax(np.array(ip)[:, 1])]
print("IP address " + result[0] + " occured " + str(result[1]) + " times.")
Out[114]: IP address IP-4 occured 3 times.
我有一个 IP 地址列表。我需要将这些 IP 地址存储在列表或数组中(不允许重复)并存储它们的数量。
例如我有
IP-1
IP-4
IP-7
IP-4
IP-2
IP-4
IP-2
此列表来自 .txt 文件。我有多个这样的文件,所以我不能像这样在代码中放置 list = [ [a,b], [c,d], [e,f] ]
(我的意思是不要在代码中静态地这样做,因为对于每个 .txt 文件,列表都会不同)。
所以在这个列表中我有:
IP-1 1x
IP-4 3x
IP-7 1x
IP-2 2x
我必须以某种方式将其全部存储在一个列表或数组中。例如像这样:
list_of_ips_and_their_counts = [ [IP-1,1], [IP-4,3], [IP-7,1], [IP-9,2] ]
现在我必须在这个list/array中搜索出现次数最多的IP并打印出来。例如:
print("IP address " + the_ip + " occured " + number + " times.") # IP address IP-4 occured 3 times.
我不确定如何存储 IP 地址及其计数。
您可以使用 collections.Counter
来计算文件中的每个 IP-{number}
行:
from collections import Counter
with open("test.txt") as f:
ip_counts = Counter(line.strip() for line in f)
# Counter({'IP-4': 3, 'IP-2': 2, 'IP-1': 1, 'IP-7': 1})
for ip_address, count in ip_counts.items():
print("IP address %s occured %d times" % (ip_address, count))
输出:
IP address IP-1 occured 1 times
IP address IP-4 occured 3 times
IP address IP-7 occured 1 times
IP address IP-2 occured 2 times
如果您愿意,也可以使用 map()
来计算行数:
ip_counts = Counter(map(str.strip, f))
注:str.strip()
这里用来去掉key的空格,比如把'IP-1\n'
转成IP-1
。由于您不需要包含空格,因此将来可以更轻松地访问密钥。
如果你想要最大计数,我会使用 max()
with operator.itemgetter()
:
print(max(ip_counts.items(), key=itemgetter(1)))
# ('IP-4', 3)
其中 returns 使用索引 1
处的计数的最大元组。
您可以使用 np.argmax()
来完成。那是你问题的第二步。但是最好每个线程问一个问题。
import numpy as np
result = ip[np.argmax(np.array(ip)[:, 1])]
print("IP address " + result[0] + " occured " + str(result[1]) + " times.")
Out[114]: IP address IP-4 occured 3 times.