每5行计算Alphabet(a,b,c)分数的脚本

时间:2014-04-25 07:54:22

标签: python python-2.7

我有一个包含IP地址列表的文件,并且需要计算每5行重复的特定IP地址。

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  

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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,并且不需要使用python,那么还有另一种非常简单的方法:

 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

答案 2 :(得分: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])