Chalarangelo / 30-seconds-of-python

Short Python code snippets for all your development needs
https://www.30secondsofcode.org/python/p/1
Creative Commons Attribution 4.0 International
8.83k stars 1.26k forks source link

[BUG] unexpected behaviour in intersection_by snippet #242

Closed azanbinzahid closed 4 years ago

azanbinzahid commented 4 years ago

The intersection_by snippet description says:

Returns a list of elements that exist in both lists, after applying the provided function to each list element of both.

As per given example:

intersection_by([2.1, 1.2], [2.3, 3.4],floor) #[2.1]

Let's dry run this, as per description given function floor will transform both lists as:

[2.1, 1.2] -> [2,1]
[2.3, 3.4] -> [2,3]

Then we take intersection between transformed list which results in [2] and return the elements from original list. Here's the catch if I am not missing anything, 2.1 from list 1 and 2.3 from list 2 has equal chances of getting in return list but former is returned. Is this behavior by design? This pattern is repeated in other list __by snippets as well.

Here are some more examples that this snippet returns:

intersection_by([2.1, 1.2], [2.5, 2.3, 3.4],floor) # [2.1]
intersection_by([2.1, 1.2, 2.5], [2.3, 3.4],floor) # [2.1, 2.5]
Trinityyi commented 4 years ago

This is by design. The snippet is supposed to by asymmetrical returning matching values in the first group after applying the transformation to both.

azanbinzahid commented 4 years ago

Then I guess it should be added to description for celerity.