将 Python 2 排序的元组列表转换为 Python 3
Convert Python 2 sorted list of tuples to Python 3
我正在将一些代码从 Python 2 转换为 Python 3。我有一个元组列表,其中每个元组包含一个数字元组和一组数字。这是一个小例子:
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}),
]
Python2 中排序的代码如下:
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
)
生成的排序列表是:
[
((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])),
]
我理解(或认为我理解)这是按集合的长度和集合比较排序。我一直在尝试转换为 Python 3,但没有成功。我首先尝试了这个:
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
)
)
这给出了一个错误,它缺少一个必需的位置参数 b,这是有道理的。然后我尝试了这个:
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
)
)
但是 returns 类型错误 'int' 的对象没有 len()。我也尝试过其他一些东西,但它们通常根本不排序。谁能帮帮我?
谢谢!
奇怪的 lambda 实际上是一种令人费解的说法:
- 1 如果
len(a[0][1]) > len(a[1][1])
- -1 如果
len(a[0][1]) < len(a[1][1])
- 否则
- 1 如果
a[0][1] > a[1][1]
- -1 如果
a[0][1] < a[1][1]
- 其他
0
所以你首先要比较元素的长度然后是它们的值(如果长度相等),所以你的键需要是这样的:
l1_python3.sort(
key=lambda a: (len(a[1]), a[1])
)
我正在将一些代码从 Python 2 转换为 Python 3。我有一个元组列表,其中每个元组包含一个数字元组和一组数字。这是一个小例子:
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}),
]
Python2 中排序的代码如下:
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
)
生成的排序列表是:
[
((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])),
]
我理解(或认为我理解)这是按集合的长度和集合比较排序。我一直在尝试转换为 Python 3,但没有成功。我首先尝试了这个:
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
)
)
这给出了一个错误,它缺少一个必需的位置参数 b,这是有道理的。然后我尝试了这个:
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
)
)
但是 returns 类型错误 'int' 的对象没有 len()。我也尝试过其他一些东西,但它们通常根本不排序。谁能帮帮我?
谢谢!
奇怪的 lambda 实际上是一种令人费解的说法:
- 1 如果
len(a[0][1]) > len(a[1][1])
- -1 如果
len(a[0][1]) < len(a[1][1])
- 否则
- 1 如果
a[0][1] > a[1][1]
- -1 如果
a[0][1] < a[1][1]
- 其他
0
- 1 如果
所以你首先要比较元素的长度然后是它们的值(如果长度相等),所以你的键需要是这样的:
l1_python3.sort(
key=lambda a: (len(a[1]), a[1])
)