Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用set()从列表中删除重复用户_Python_Python 2.7_Set - Fatal编程技术网

Python 使用set()从列表中删除重复用户

Python 使用set()从列表中删除重复用户,python,python-2.7,set,Python,Python 2.7,Set,正在尝试使用python中的set从列表中删除重复用户。问题是它没有删除重复用户: with open ('live.txt') as file: for line in file.readlines(): word = line.split() users = (word[word.index('user')+1]) l = users.split()

正在尝试使用python中的set从列表中删除重复用户。问题是它没有删除重复用户:

with open ('live.txt') as file: 
        for line in file.readlines(): 
                word = line.split()
                users = (word[word.index('user')+1]) 
                        l  = users.split() 
                        l = set(l)
                        l = sorted(l)
                        print " ".join(l)
以下是live.txt的内容:

Sep 15 04:34:24 li146-252 sshd[13320]:无效用户ronda的密码失败,来自212.58.111.170端口42201 ssh2 Sep 15 04:34:26 li146-252 sshd[13322]:无效用户ronda的密码失败,来自212.58.111.170端口42330 ssh2 Sep 15 04:34:28 li146-252 sshd[13324]:无效用户ronda的密码失败,来自212.58.111.170端口42454 ssh2 Sep 15 04:34:31 li146-252 sshd[13326]:无效用户ronda的密码失败,来自212.58.111.170端口42579 ssh2 Sep 15 04:34:33 li146-252 sshd[13328]:来自212.58.111.170端口42715 ssh2的无效用户romero的密码失败 Sep 15 04:34:36 li146-252 sshd[13330]:来自212.58.111.170端口42838 ssh2的无效用户romero的密码失败
您可以尝试一种更简单的方法

list(set(<Your user list>))
我希望这能解决你的问题:

import re
def remove_me():
    all_users = []
    with open ('live.txt') as file:
        for line in file.readlines():
            pattern = re.compile('(.*user\s*)([a-zA-Z0-9]*)')
            stmt = pattern.match(line)
            all_users.append(stmt.groups()[1])
    unique_users = list(set(all_users))
    print unique_users

if __name__ == "__main__":
    remove_me()

以下是您需要的代码:

with open ('live.txt') as file: 
    users = []
    for line in file.readlines(): 
        word = line.split()
        users.append(word[word.index('user') + 1])
    unique_users = list(set(users))
print " ".join(unique_users)
输出:

romero ronda

如果重复的用户行是连续的;您可以使用删除重复项:

#!/usr/bin/env python
from itertools import groupby
from operator import itemgetter

def extract_user(line):
    return line.partition('user')[2].partition('from')[0].strip()

with open('live.txt') as file:
    print(" ".join(map(itemgetter(0), groupby(file, key=extract_user))))
    # -> ronda romero

这应该是一次性活动。不需要循环。请为用户添加示例值!你介意在这里添加你的用户吗。以及预期的输出将随整个代码更新@Optimus您是否希望从live.txt获取所有唯一的用户?因此,从上面的屏幕上,你想要[ronda,romero]这是我得到的回报:[a',e',k',m',3',p',s',t']['a',e',k',m',3',p',s',t']['a',e',k',m',3',p',k',s',t']['a',e',k',m',3',p',s',t']我忘了提到每个用户都在自己的列表中。如果您想使用字典来计算用户出现的次数,该怎么办?@user3270211:请不要用于文件中的行。请使用readlines,而是用于文件中的行。顺便说一句,单词是误读的-应该是单词。您不需要在这里调用列表。@user3270211只需控制用户是否在dict中。如果用户不在dict中,请将值1添加到dict[user]。如果它们已经在dict中,请将值更改为dict[user]+1。
#!/usr/bin/env python
from itertools import groupby
from operator import itemgetter

def extract_user(line):
    return line.partition('user')[2].partition('from')[0].strip()

with open('live.txt') as file:
    print(" ".join(map(itemgetter(0), groupby(file, key=extract_user))))
    # -> ronda romero