Issue
I'm still pretty new to python and oop and I have some struggles resolving this problem without breaking the performance.
I want to compare the id of my user (that's what I have done with the eq function) and if the id is equal I want to know if their time attribute is greater than the other user who the id is the same
I retrieve my user from a different source, that's why I have to compare them.
class User:
def __init__(self,id: str, time: int) -> None:
self.id = id
self.time = time
def __eq__(self, __o: object) -> bool:
return self.id == __o.id
list_user= [User(1, 20),User(2, 20),User(3, 45),...]
list_user2=[User(1, 5),User(4323, 20),User(3, 60),...]
for user in list_user:
if user.id in list_user2 and user.time > list_user2:
do_something()
else:
continue
Can I retrieve the user that matches the user in user_list2 in the first condition to compare their times attribute?
How should I approach this problem?
Solution
class User:
def __init__(self, _id: str, time: int) -> None:
self.id = _id
self.time = time
list_user = [User(1, 20), User(2, 20), User(3, 45), ...]
list_user2 = [User(1, 5), User(4323, 20), User(3, 60), ...]
list_user2_ids = [u.id for u in list_user2]
for user in list_user:
try:
identical_user = list_user2[list_user2_ids.index(user.id)]
if user.time > identical_user.time:
do_something()
except:
pass
Answered By - Omer Glazer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.