将2个值放入一个dict python
嘿,我想把两个值放到一个dict中,但不确定如何实现这一点。我希望它看起来像这样将2个值放入一个dict python,python,Python,嘿,我想把两个值放到一个dict中,但不确定如何实现这一点。我希望它看起来像这样 {'Feb 7': {'89.249.209.92': 15}, 'Feb 8': {'66.30.90.148': 14, '72.153.93.203': 14, '92.152.92.123': 5}, 'Jan 10': {'213.251.192.26': 13, '218.241.173.35': 15}} 这里是我目前的代码和我目前在dict中得到的内容 desc_ip = {} coun
{'Feb 7': {'89.249.209.92': 15},
'Feb 8': {'66.30.90.148': 14, '72.153.93.203': 14, '92.152.92.123': 5},
'Jan 10': {'213.251.192.26': 13, '218.241.173.35': 15}}
这里是我目前的代码和我目前在dict中得到的内容
desc_ip = {}
count_ip = 0
for line in myfile:
if 'Failed password for' in line:
line_of_list = line.split()
ip_address = ' '.join(line_of_list[0:2])
ip_address = line_of_list[-4]
if ip_address in desc_ip:
count_ip = desc_ip[ip_address]
count_ip = count_ip +1
desc_ip[ip_address] = count_ip
#zero out the temporary counter as a precaution
count_ip =0
else:
desc_ip[ip_address] = 1
for ip in desc_ip.keys():
print ip ,' has', desc_ip[ip] , ' attacks'
这是我目前的口述
{'213.251.192.26': 13,
'218.241.173.35': 15,
'66.30.90.148': 14,
'72.153.93.203': 14,
'89.249.209.92': 15,
'92.152.92.123': 5}
这里是文件中的几行
Jan 10 09:32:09 j4-be03 sshd[3876]: Failed password for root from 218.241.173.35 port 47084 ssh2
Feb 7 17:19:24 j4-be03 sshd[10740]: Failed password for root from 89.249.209.92 port 46752 ssh2
您需要一个dict of dict,因此使用日期作为键,并为每个日期创建一个dict。您需要一个dict of dict,因此使用日期作为键,并为每个日期创建一个dict。非常可扩展的答案使用Autovification类:
date = 'Feb 7'
ip = '89.249.209.92'
count = 15
d = {}
d[date] = {ip: count}
使用一些示例数据:
sample_text = '''Feb 7 : 89.249.209.92
Feb 7 : 89.249.209.92
Feb 7 : 89.249.209.92
Feb 8 : 89.249.209.92
Jan 10 : 218.241.173.35'''
快速读取数据可得出:
A = AutoVivification()
for line in sample_text.split('\n'):
date, IP = map(str.strip,line.split(':'))
if IP not in A[date]: A[date][IP] = 0
A[date][IP] += 1
>>>> {'Jan 10': {'218.241.173.35': 1}, 'Feb 8': {'89.249.209.92': 1}, 'Feb 7': {'89.249.209.92': 3}}
请注意,AutoVivification类不知道嵌套的深度。因此,您需要通过显式设置键的值来设置深度,如
a[date][IP]=0
非常可扩展的答案使用Autovification类:
使用一些示例数据:
sample_text = '''Feb 7 : 89.249.209.92
Feb 7 : 89.249.209.92
Feb 7 : 89.249.209.92
Feb 8 : 89.249.209.92
Jan 10 : 218.241.173.35'''
快速读取数据可得出:
A = AutoVivification()
for line in sample_text.split('\n'):
date, IP = map(str.strip,line.split(':'))
if IP not in A[date]: A[date][IP] = 0
A[date][IP] += 1
>>>> {'Jan 10': {'218.241.173.35': 1}, 'Feb 8': {'89.249.209.92': 1}, 'Feb 7': {'89.249.209.92': 3}}
请注意,AutoVivification类不知道嵌套的深度。因此,您需要通过显式设置键的值来设置深度,如
a[date][IP]=0
我假设每行中都有日期:
>>> sample_text = '''Feb 7 : Failed password for 89.249.209.92
Feb 7 : Failed password for 89.249.209.92
Feb 7 : Failed password for 89.249.209.92
Feb 8 : Failed password for 89.249.209.92
Jan 10 : Failed password for 218.241.173.35'''
>>> desc_ip = {}
>>> for line in sample_text.split('\n'):
if 'Failed password for' in line:
line_of_list = line.split()
ip_address = line_of_list[6]
date = ' '.join(line_of_list[0:2])
if not date in desc_ip:
desc_ip[date] = {}
if not ip_address in desc_ip[date]:
desc_ip[date][ip_address] = 0
desc_ip[date][ip_address] += 1
>>> desc_ip
{'Jan 10': {'218.241.173.35': 1}, 'Feb 8': {'89.249.209.92': 1}, 'Feb 7': {'89.249.209.92': 3}}
>>>
我可以使用
defaultdict
来避免测试,例如如果不是desc\u ip中的date
,但是这里我们需要defaultdict
的defaultdict
(比如desc\u ip=defaultdict(lambda:defaultdict(int))
)和IMHO,这会降低可读性。我假设每一行都有日期:
>>> sample_text = '''Feb 7 : Failed password for 89.249.209.92
Feb 7 : Failed password for 89.249.209.92
Feb 7 : Failed password for 89.249.209.92
Feb 8 : Failed password for 89.249.209.92
Jan 10 : Failed password for 218.241.173.35'''
>>> desc_ip = {}
>>> for line in sample_text.split('\n'):
if 'Failed password for' in line:
line_of_list = line.split()
ip_address = line_of_list[6]
date = ' '.join(line_of_list[0:2])
if not date in desc_ip:
desc_ip[date] = {}
if not ip_address in desc_ip[date]:
desc_ip[date][ip_address] = 0
desc_ip[date][ip_address] += 1
>>> desc_ip
{'Jan 10': {'218.241.173.35': 1}, 'Feb 8': {'89.249.209.92': 1}, 'Feb 7': {'89.249.209.92': 3}}
>>>
我可以使用
defaultdict
来避免测试,例如if not date in desc_ip
,但是这里我们需要defaultdict
的defaultdict
(比如desc_ip=defaultdict(lambda:defaultdict(int))
)和IMHO,这会降低可读性。您能发布几行示例输入吗(myfile
),为了更方便地使用您的代码段?请查看以下内容:[Python嵌套字典][1][1]:能否发布几行示例输入(myfile
),以便更方便地使用您的代码段?请查看以下内容:[Python嵌套字典][1][1]: