简体   繁体   中英

Python - Remove specific object in list by condition

I have a class "PushInfo"
And generate 300 PushInfo object in list
I want remove duplicate userid and ip in the list

Here is my code:

from faker import Faker
import random

def RemovePustListDuplicateData(PushList):
    return  list(set([(x.userid, x.ip) for x in PushList]))

def FakeData(number):
  PushList = []
  fake = Faker()
  accountList = [('john','127.0.0.1'),('john','127.0.0.1'),('amy','127.0.0.1'),
                 ('lia','140.112.1.9'),('julia','140.112.1.9'),
                 ('asuka','140.112.1.9'),('roy','140.112.1.9'),('stacie','140.112.1.9'),('ben','123.964.123.41'),
                 ('yich','127.0.0.1'),('beef','127.0.0.1'),('aloha','235.151.123.1'),('yamaha','235.151.123.1')]
  for i in range(0,number):
      user = random.choice(accountList)
      PushList.append(PushInfo(fake.name(),
                                     user[0],
                                     fake.text(max_nb_chars=10),
                                     fake.date(pattern="%Y-%m-%d"),
                                     user[1]
                                     ))

  return PushList


class PushInfo:
    def __init__(self, name, userid, content, time,ip=''):
        self.name = name
        self.userid = userid
        self.content = content
        self.time = time
        self.ip = ip


PushList = FakeData(300)
print("top 10 push in list:")
for push in PushList[:10]:
  print("name:"+push.name+" id:"+push.userid+" content:"+push.content+" time:"+push.time+" ip:"+push.ip)

print("\nremove duplicate userid and ip  data")
print(RemovePustListDuplicateData(PushList))

https://repl.it/@YichLin/Remove-object-in-list/

The example code is return tuple list

[(userid,ip),(userid,ip)....]

But the result I want is

[PushInfo(some data),PushInfo(some data),.....]

How to achieve this result?

Try this:

from faker import Faker
import random

def RemovePustListDuplicateData(PushList):
    return  list(set(PushList))

def FakeData(number):
  PushList = []
  fake = Faker()
  accountList = [('john','127.0.0.1'),('john','127.0.0.1'),('amy','127.0.0.1'),
                 ('lia','140.112.1.9'),('julia','140.112.1.9'),
                 ('asuka','140.112.1.9'),('roy','140.112.1.9'),('stacie','140.112.1.9'),('ben','123.964.123.41'),
                 ('yich','127.0.0.1'),('beef','127.0.0.1'),('aloha','235.151.123.1'),('yamaha','235.151.123.1')]
  for i in range(0,number):
      user = random.choice(accountList)
      PushList.append(PushInfo(fake.name(),
                                     user[0],
                                     fake.text(max_nb_chars=10),
                                     fake.date(pattern="%Y-%m-%d"),
                                     user[1]
                                     ))

  return PushList


class PushInfo:
    def __init__(self, name, userid, content, time,ip=''):
        self.name = name
        self.userid = userid
        self.content = content
        self.time = time
        self.ip = ip

    def __eq__(self, other):
      return self.userid==other.userid and self.ip==other.ip

    def __hash__(self):
      return hash(('userid', self.userid, 'ip', self.ip))

    def __repr__(self):
      return str(self.userid) + ' ' + str(self.ip)


PushList = FakeData(300)
print("top 10 push in list:")
for push in PushList[:10]:
  print("name:"+push.name+" id:"+push.userid+" content:"+push.content+" time:"+push.time+" ip:"+push.ip)

print("\nremove duplicate userid and ip  data")
print(RemovePustListDuplicateData(PushList))

You need to implement eq and hash methods in order to check whether two objects are same.

Change the RemovePustListDuplicateData(PushList) function as follows:-

def RemovePustListDuplicateData(PushList):
    object_memo = set()
    final_list = []
    for object in PushList:
        if (object.userid, object.ip) in object_memo:
            continue
        else:
            final_list.append(object)
            object_memo.add((object.userid, object.ip))
     return final_list

I hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM