2014-04-25 70 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 
相關問題