#π equality of two lists by function
24 messages Β· Page 1 of 1 (latest)
@balmy vortex
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
call the lambda with both lists as arguments
sorry, can you please help me with the syntax?
given as: List[Foo], bs: List[Foo] and a method matches(other: Foo) -> bool defined on Foo, how would I write this?
I see all(map(Foo.matches, as, bs))
would work
is other supposed to be a method on Foo?
if I also check len(as) == len(bs)
It should be accepting self
yes it is
so len(as) == len(bs) and all(map(Foo.matches, as, bs)) would work, but (coming from other languages), it feels like there's an existing way to do this
i believe as == bs should work if you override __eq__ on Foo
there's really nothing else, huh?
are you expecting something specific?
yes. I expect any reasonable language to give me a compare_by function for iterables
thank you for the help
!e @balmy vortex
class Foo:
def __init__(self, value: int) -> None:
self.value = value
def __eq__(self, other) -> bool:
if isinstance(other, Foo):
return self.value == other.value
return False
xs = [Foo(1), Foo(2)]
ys = [Foo(1), Foo(2)]
print(xs == ys)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
True
that assumes I control the definition of Foo and that I want to define general equality as matches
right. otherwise we don't have a compare_by because genexps are flexible enough to cover that usecase
I'm going to have to get used to this way of doing things.
this is my language unfamiliarity + personal taste speaking here.
I'm sure I'll get over it π
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.