,如果你有一個簡單的方法將你的文件讀入python列表就是使用集合庫中的Counter函數。
我做了一個簡單的例子:
from collections import Counter
from pprint import print
#I've just put this here for showing how it works. you can replace this with
#reading the data from a file
ips = ['74.125.227.31', '74.125.229.87', '173.194.39.56', '173.194.39.56', '74.125.232.216', '173.194.39.56', '74.125.239.31', '173.194.39.56', '74.125.227.31', '74.125.227.31', '74.125.239.23', '173.194.34.120', '74.125.227.31', '74.125.239.23', '74.125.239.23']
#this is an example how you can read the lines from your file. just replace the file name
ips = [line.strip() for line in open('ip.txt')]
#this does the magic: Counter(ips)
pprint (Counter(ips))
# and this is the result as a dict
{'173.194.34.120': 1,
'173.194.39.56': 4,
'74.125.227.31': 4,
'74.125.229.87': 1,
'74.125.232.216': 1,
'74.125.239.23': 3,
'74.125.239.31': 1}`
如果你是在Linux或UNIX和東西並不需要在蟒蛇還有另外一個很簡單的方法來做到這一點:
cat ip.txt | tr -d ' '| sort | uniq -c | sort -n
1 173.194.34.120
1 74.125.229.87
1 74.125.232.216
1 74.125.239.31
3 74.125.239.23
4 173.194.39.56
4 74.125.227.31
我不明白你的問題。請更改/解釋 – sshashank124
字母(a,b,c)是什麼意思?它只有一個IP地址,你想要計數? –
其實,這不是一個代碼生成器 - 你有什麼嘗試過自己? – JeffRSon