循环递增字典:
Increment dictionary in a loop:
我需要计算给定数字 os 文本模式在日志文件中出现的次数,并将其存储在字典中。
我的问题是我的代码将文件的所有条目计算为每种文本模式。
日志文件如下所示:
我做错了什么?
>Feb 1 00:00:02 bridge kernel: INBOUND TCP: IN=br0 PHYSIN=eth0 OUT=br0 >PHYSOUT=eth1 SRC=XXX.XXX.XXX.XXX DST=XXX.XXX.XXX.XXX LEN=40 TOS=0x00 >PREC=0x00 TTL=110 ID=12973 PROTO=TCP SPT=220 DPT=6129 WINDOW=16384 RES=0x00 >SYN URGP=0
>Feb 1 00:00:02 bridge kernel: INBOUND TCP: IN=br0 PHYSIN=eth0 OUT=br0 >PHYSOUT=eth1 SRC=XXX.XXX.XXX.XXX DST=XXX.XXX.XXX.XXX LEN=40 TOS=0x00 >PREC=0x00 TTL=113 ID=27095 PROTO=TCP SPT=220 DPT=6129 WINDOW=16384 RES=0x00 >SYN URGP=0
我目前的代码是这样的:
#!//usr/bin/python3
import sys
import os
import re
from collections import defaultdict
tipos={}
p= re.compile ('bridge kernel:.*:')
with open (sys.argv[1], 'r') as f:
for line in f:
match = p.search(line)
if match:
taux=(line.split(":") [3])
tipos[taux]=1
print (tipos)
代码没有报错,但是所有key都有保存值
我读过 defaultdict
和 Counters
但无法使它们起作用。
请帮帮我。
您想使用默认字典:
tipos = defaultdict(int)
p= re.compile ('bridge kernel:.*:')
with open (sys.argv[1], 'r') as f:
for line in f:
match = p.search(line)
if match:
taux=(line.split(":") [3])
tipos[taux] += 1
print (tipos)
你在那里导入了它但你没有使用它
至于你的代码版本,你从来没有增加tipos中taux的数量,所以它们应该都是一个。是的,defaultdicts 会有所帮助,因为它们会使用您传入的类型自动实例化缺少的字典条目。一般的 defaultdict 计数模式如下:
a = defaultdict(int)
a['asdf'] += 1
# a['asdf'] will now be 1, since it updates from 0
编辑:包括@Jean-FrançoisFabre 评论,我想指出 collections
模块带有一个专门设计用于计算任何可哈希值的对象 - 计数器。从外观上看,它依赖于大部分相同的后端,因此性能应该相似,但它带有一些不错的小额外功能(如 most_common(number_of_most_common_elements)
方法。这可以像 defaultdict 一样使用,但没有专用 (int)
参数:
a = Counter()
a['asdf'] += 1
# a['asdf'] will now be 1, since it updates from 0
一般来说,传递的每个参数都会对应一个默认值。这意味着您也可以执行以下操作:
a = defaultdict(int)
print(a['asdf']) # will print 0
a = defaultdict(float)
print(a['asdf']) # will print 0.0
a = defaultdict(list)
print(a['asdf']) # will print [], and is particularly useful if you want a dict of lists, since you don't need to check whether your key already exists in the dict
至于你的代码,这意味着你想要:
tipos=defaultdict(int)
p= re.compile ('bridge kernel:.*:')
with open (sys.argv[1], 'r') as f:
for line in f:
match = p.search(line)
if match:
taux=(line.split(":") [3])
tipos[taux]+=1
print (tipos)
我需要计算给定数字 os 文本模式在日志文件中出现的次数,并将其存储在字典中。
我的问题是我的代码将文件的所有条目计算为每种文本模式。
日志文件如下所示:
我做错了什么?
>Feb 1 00:00:02 bridge kernel: INBOUND TCP: IN=br0 PHYSIN=eth0 OUT=br0 >PHYSOUT=eth1 SRC=XXX.XXX.XXX.XXX DST=XXX.XXX.XXX.XXX LEN=40 TOS=0x00 >PREC=0x00 TTL=110 ID=12973 PROTO=TCP SPT=220 DPT=6129 WINDOW=16384 RES=0x00 >SYN URGP=0
>Feb 1 00:00:02 bridge kernel: INBOUND TCP: IN=br0 PHYSIN=eth0 OUT=br0 >PHYSOUT=eth1 SRC=XXX.XXX.XXX.XXX DST=XXX.XXX.XXX.XXX LEN=40 TOS=0x00 >PREC=0x00 TTL=113 ID=27095 PROTO=TCP SPT=220 DPT=6129 WINDOW=16384 RES=0x00 >SYN URGP=0
我目前的代码是这样的:
#!//usr/bin/python3
import sys
import os
import re
from collections import defaultdict
tipos={}
p= re.compile ('bridge kernel:.*:')
with open (sys.argv[1], 'r') as f:
for line in f:
match = p.search(line)
if match:
taux=(line.split(":") [3])
tipos[taux]=1
print (tipos)
代码没有报错,但是所有key都有保存值
我读过 defaultdict
和 Counters
但无法使它们起作用。
请帮帮我。
您想使用默认字典:
tipos = defaultdict(int)
p= re.compile ('bridge kernel:.*:')
with open (sys.argv[1], 'r') as f:
for line in f:
match = p.search(line)
if match:
taux=(line.split(":") [3])
tipos[taux] += 1
print (tipos)
你在那里导入了它但你没有使用它
至于你的代码版本,你从来没有增加tipos中taux的数量,所以它们应该都是一个。是的,defaultdicts 会有所帮助,因为它们会使用您传入的类型自动实例化缺少的字典条目。一般的 defaultdict 计数模式如下:
a = defaultdict(int)
a['asdf'] += 1
# a['asdf'] will now be 1, since it updates from 0
编辑:包括@Jean-FrançoisFabre 评论,我想指出 collections
模块带有一个专门设计用于计算任何可哈希值的对象 - 计数器。从外观上看,它依赖于大部分相同的后端,因此性能应该相似,但它带有一些不错的小额外功能(如 most_common(number_of_most_common_elements)
方法。这可以像 defaultdict 一样使用,但没有专用 (int)
参数:
a = Counter()
a['asdf'] += 1
# a['asdf'] will now be 1, since it updates from 0
一般来说,传递的每个参数都会对应一个默认值。这意味着您也可以执行以下操作:
a = defaultdict(int)
print(a['asdf']) # will print 0
a = defaultdict(float)
print(a['asdf']) # will print 0.0
a = defaultdict(list)
print(a['asdf']) # will print [], and is particularly useful if you want a dict of lists, since you don't need to check whether your key already exists in the dict
至于你的代码,这意味着你想要:
tipos=defaultdict(int)
p= re.compile ('bridge kernel:.*:')
with open (sys.argv[1], 'r') as f:
for line in f:
match = p.search(line)
if match:
taux=(line.split(":") [3])
tipos[taux]+=1
print (tipos)