#πŸ”’ equality of two lists by function

24 messages Β· Page 1 of 1 (latest)

balmy vortex
#

I've got two lists, and I'd like to check that they are equal according to some custom lambda. how can I do this?

sweet bisonBOT
#

@balmy vortex

Python help channel opened

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.

torpid field
balmy vortex
#

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

torpid field
balmy vortex
#

if I also check len(as) == len(bs)

torpid field
#

It should be accepting self

balmy vortex
#

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

torpid field
balmy vortex
#

there's really nothing else, huh?

torpid field
balmy vortex
#

yes. I expect any reasonable language to give me a compare_by function for iterables

#

thank you for the help

torpid field
#

!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)
sweet bisonBOT
balmy vortex
#

that assumes I control the definition of Foo and that I want to define general equality as matches

torpid field
balmy vortex
#

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 πŸ‘

sweet bisonBOT
#
Python help channel closed using Discord native close action

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.