2

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?

2
  • Can we assume that the ids in each list are unique (unique within its list)? Commented Oct 12, 2022 at 8:47
  • 1
    yes, every id is unique within its list Commented Oct 12, 2022 at 9:00

3 Answers 3

2
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
Sign up to request clarification or add additional context in comments.

2 Comments

@bakelue did you try to time this solution ?
how do you call this kind of alghoritm approach ?? its really fast compared to a for i in list1 if/if by at least 20times by my tests ?
0
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


User(1, 20)
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 in list_user2:
        if user.time>list_user2[list_user2.index(user)].time:
            print(f"list1's user {user.id} time is bigger")
        else:
            print(f"list2's user {user.id} time is bigger")
    else:
        print("not found")

In line 17 I've just use the index of the first list's user to get the time value of the second lists user with the same id, then just compared it OUTPUT

list1's user 1 time is bigger
not found
list2's user 3 time is bigger

2 Comments

Yours solution work, but it's pretty slow, it take me 340s for 13240 match in the lists. I don't know where the slowdown occur
Its probably in the the index retrieval in the 2nd if check.
0

modifying __eq__ , not sure it is orthodox:

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 and self.time > __o.time

list_user = [User(1, 20), User(2, 20), User(3, 45), User(4,99)]
list_user2 = [User(1, 5), User(4323, 20), User(3, 60), User(4,98)]

for user in list_user:
        print(user)
        if user in list_user2:
                print('OK ' ,user.id)
        else:
            print('pass')
            pass

output:

<__main__.User object at 0x7fafc54554c0>
OK  1
<__main__.User object at 0x7fafc52ff130>
pass
<__main__.User object at 0x7fafc52ff220>
pass
<__main__.User object at 0x7fafc52ff370>
OK  4
> 

1 Comment

@bakelue did you try to time this solution ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.