Issue
I'm converting some code from Python 2 to Python 3. I have a list of tuples, where each tuple contains a tuple of numbers and a set of numbers. Here's a small example:
l1_python2 = [
((8, 6), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((8, 7), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((0, 3), Set([1, 2, 5, 6, 7, 9])),
((0, 4), Set([1, 2, 5, 6, 7, 9])),
((0, 5), Set([1, 2, 5, 6, 7, 9])),
((0, 6), Set([1, 2, 5, 6, 7, 9])),
((0, 7), Set([1, 2, 5, 6, 7, 9])),
((0, 8), Set([1, 2, 5, 6, 7, 9])),
((1, 0), Set([1, 2, 5, 6, 7, 9])),
((8, 8), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((5, 3), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((5, 4), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((5, 5), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((5, 6), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
]
l1_python3 = [
((8, 6), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
((8, 7), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
((0, 3), {1, 2, 5, 6, 7, 9}),
((0, 4), {1, 2, 5, 6, 7, 9}),
((0, 5), {1, 2, 5, 6, 7, 9}),
((0, 6), {1, 2, 5, 6, 7, 9}),
((0, 7), {1, 2, 5, 6, 7, 9}),
((0, 8), {1, 2, 5, 6, 7, 9}),
((1, 0), {1, 2, 5, 6, 7, 9}),
((8, 8), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
((5, 3), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
((5, 4), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
((5, 5), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
((5, 6), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
]
The code to sort in Python 2 is the following:
l1_python2.sort(
lambda a, b: len(a[1]) > len(b[1])
and 1
or len(a[1]) < len(b[1])
and -1
or a[0] > b[0]
and 1
or a[1] < b[1]
and -1
or 0
)
The resulting sorted list is:
[
((0, 3), Set([1, 2, 5, 6, 7, 9])),
((0, 4), Set([1, 2, 5, 6, 7, 9])),
((0, 5), Set([1, 2, 5, 6, 7, 9])),
((0, 6), Set([1, 2, 5, 6, 7, 9])),
((0, 7), Set([1, 2, 5, 6, 7, 9])),
((0, 8), Set([1, 2, 5, 6, 7, 9])),
((1, 0), Set([1, 2, 5, 6, 7, 9])),
((8, 6), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((8, 7), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((8, 8), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((5, 3), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((5, 4), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((5, 5), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
((5, 6), Set([1, 2, 3, 4, 5, 6, 7, 8, 9])),
]
I understand (or think that I do) that's it sorting by the length of the sets and by set comparison. I've been trying to convert to Python 3 with no luck though. I tried this first:
l1_python3.sort(
key=(
lambda a, b: len(a[1]) > len(b[1])
and 1
or len(a[1]) < len(b[1])
and -1
or a[0] > b[0]
and 1
or a[1] < b[1]
and -1
or 0
)
)
That gives an error that it's missing one required positional argument b, which makes sense. I then tried this:
l1_python3.sort(
key=(
lambda a: len(a[0][1]) > len(a[1][1])
and 1
or len(a[0][1]) < len(a[1][1])
and -1
or a[0][1] > a[1][1]
and 1
or a[0][1] < a[1][1]
and -1
or 0
)
)
But that returns a TypeError that object of type 'int' has no len(). I've tried a few other things also but they usually don't sort at all. Can anyone help me out?
Thanks!
Solution
The weird lambda is actually a convoluted way of saying:
- 1 if
len(a[0][1]) > len(a[1][1])
- -1 if
len(a[0][1]) < len(a[1][1])
- otherwise
- 1 if
a[0][1] > a[1][1]
- -1 if
a[0][1] < a[1][1]
- else
0
- 1 if
So you first want to compare length of elements then theirs values (if length are equals), so your key needs to be something like:
l1_python3.sort(
key=lambda a: (len(a[1]), a[1])
)
Answered By - Holt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.