[英]Python - Remove specific object in list by condition
I have a class "PushInfo" 我有一类“ PushInfo”
And generate 300 PushInfo object in list 并在列表中生成300个PushInfo对象
I want remove duplicate userid and ip in the list 我想删除列表中重复的用户ID和IP
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/ 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. 您需要实现eq和hash方法,以检查两个对象是否相同。
Change the RemovePustListDuplicateData(PushList) function as follows:- 更改RemovePustListDuplicateData(PushList)函数,如下所示:
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! 希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.