gpt4 book ai didi

python - 如何将此项目 append 到同一列表中,而不是每次循环时创建一个新列表?

转载 作者:太空宇宙 更新时间:2023-11-03 19:48:42 28 4
gpt4 key购买 nike

这是我当前的代码:

fd = open('/var/log/secure.log')
syslog = fd.readlines()

countTable = {} # initialization of a dictionary
for line in syslog:
data = line.split()
if len(data) >= 15:
if data[14] in ['user=root']:
intrusions = []
ip = data[13].replace('rhost=', "")
intrusions.extend(ip)
print "intruder:", intrusions

这是我得到的输出:

intruder: ['218.92.0.201']
intruder: ['222.186.173.142']
intruder: ['222.186.173.142']
intruder: ['222.186.173.142']
intruder: ['218.92.0.201']
intruder: ['218.92.0.201']
intruder: ['222.186.30.218']
intruder: ['218.92.0.201']

如何将所有这些 IP 放入同一个“入侵”列表中?

谢谢!

最佳答案

您只需在第一个循环之前移动您的intrusions = []:

countTable = {} # initialization of a dictionary

intrusions = []
for line in syslog:
data = line.split()
if len(data) >= 15:
if data[14] in ['user=root']:

ip = data[13].replace('rhost=', "")
intrusions.extend(ip)
print "intruder:", intrusions

关于python - 如何将此项目 append 到同一列表中,而不是每次循环时创建一个新列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59974027/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com