,如果你有一个简单的方法将你的文件读入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