2014-04-25 90 views
0

我有一个包含IP地址列表的文件,并且需要对每5行重复的特定IP地址进行计数。脚本每5行计算字母表(a,b,c)的分数

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  

预期成果是:(每隔五线它计数数量173.194.39.56重复我的意思是在上面的列表中,在前五线的IP地址173.194.39.56重复2次,并在十二五线就重复。 2倍,而在过去的五年线就会发现零次)

IP Address      count 
173.194.39.56     2  
173.194.39.56     2 
173.194.39.56     0 
+0

我不明白你的问题。请更改/解释 – sshashank124

+0

字母(a,b,c)是什么意思?它只有一个IP地址,你想要计数? –

+0

其实,这不是一个代码生成器 - 你有什么尝试过自己? – JeffRSon

回答

0
from collections import Counter 

data = ['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'] 

ip = '173.194.39.56' 
formatstr = "{:<16}{:>8}" 
print formatstr.format('IP Address', 'count') 

paginated = [data[start:end] for start, end in 
     zip(range(0,len(data),5), range(5, len(data), 5)+[None])] 
for chunk in paginated: 
    print formatstr.format(ip, Counter(chunk)[ip]) 
1

下面的代码工作:

with open('input.txt') as fl: 
    f = fl.read().split() 

f = [f[i:i+5] for i in range(0,len(f),5)] 

s = '173.194.39.56' 

for i in f: 
    print i.count(s) 

[OUTPUT] 
2 
2 
0 
0

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