2015-02-24 25 views
-3

試圖從在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]: Failed password for invalid user ronda from 212.58.111.170 port 42201 ssh2 
Sep 15 04:34:26 li146-252 sshd[13322]: Failed password for invalid user ronda from 212.58.111.170 port 42330 ssh2 
Sep 15 04:34:28 li146-252 sshd[13324]: Failed password for invalid user ronda from 212.58.111.170 port 42454 ssh2 
Sep 15 04:34:31 li146-252 sshd[13326]: Failed password for invalid user ronda from 212.58.111.170 port 42579 ssh2 
Sep 15 04:34:33 li146-252 sshd[13328]: Failed password for invalid user romero from 212.58.111.170 port 42715 ssh2 
Sep 15 04:34:36 li146-252 sshd[13330]: Failed password for invalid user romero from 212.58.111.170 port 42838 ssh2 
+1

這應該一次性activity.There不應該需要一個循環 – vks 2015-02-24 08:47:42

+0

爲'users'請新增樣本值! – 2015-02-24 08:47:43

+1

你介意在這裏添加你的用戶嗎?預計輸出什麼 – 2015-02-24 08:52:39

回答

1

這裏是代碼,你想:

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 
+0

如果您想使用詞典來計算用戶出現次數,它將會如何? – user3270211 2015-02-24 09:42:36

+0

@ user3270211:請不要在file.readlines()中使用'for line,而是使用'for line in file'。順便說一下,「單詞」有誤導性 - 它應該是「單詞」。你不需要在這裏調用list()。 – jfs 2015-02-24 10:04:56

+0

@ user3270211只要用戶在字典中,就可以進行控制。如果他們不在字典中,請爲[dict] [用戶]添加值1。如果它們已經在詞典中,請將該值更改爲dict [user] + 1。 – Noyan 2015-02-24 12:14:59

1

你可以嘗試一個更簡單的方式

list(set(<Your user list>)) 

這將返回列表中沒有重複。 Python的數據類型爲set,它是唯一元素的集合。因此,只要通過類型轉換你的listset會自動刪除重複的

例子:

>>> users = ['john', 'mike', 'ross', 'john','obama','mike'] 
>>> list(set(users)) 
['mike', 'john', 'obama', 'ross'] 
>>> 

我希望這將解決您的問題:

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() 
+0

這就是我得到的回報:['a','e','k','m','3','p','s','t'] ['a', 'e','k','m','3','p','s','t'] ['a','e','k','m','3', '','s','t'] ['a','e','k','m','3','p','s','t'] ['a ','e','k','m','3','p','s','t'] – user3270211 2015-02-24 08:49:49

+0

@ user3270211好悲痛,_say_你的輸入數據是什麼! – 2015-02-24 08:51:24

+0

我忘了提及每個用戶都在自己的列表中。 – user3270211 2015-02-24 09:01:43

0

如果重複的用戶行是連續的;你可以使用itertools.groupby()刪除重複:

#!/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