英文:
How to sort a a Python List on two fields
问题
我已经使用以下代码合并了2个列表项。例如,列表'c'是列表变量'a'和'b'的创建。
a = [['2022', 4], ['2023', 5]]
b = ["Random1", "Random2"]
c = list(zip(a, b))
因此,'c'看起来像这样:
Out[]: [(['2022', 4], 'Random1'), (['2023', 5], 'Random2')]
我尝试对'c'的第二列和第三列进行排序,即:
c_sorted = c.sort(key=lambda i: (i[1], i[2]))
但是我收到了错误消息:
IndexError: tuple index out of range
任何帮助将不胜感激!谢谢!
英文:
I have used the following code to merge 2 list items. For example, list 'c' is a creation of list variables 'a' and 'b'
a=[['2022', 4], ['2023', 5]]
b=["Random1","Random2"]
c=list(zip(a,b))
'c' therefore looks like this
Out[]: [(['2022', 4], 'Random1'), (['2023', 5], 'Random2')]
i tried to sort 'c' on the 2nd and 3rd column i.e.
c_sorted=c.sort(key=lambda i:(i[1],i[2]))
however i get the error
IndexError: tuple index out of range
Any help would be greatly appreciated! thanks!
答案1
得分: 1
以下是翻译好的部分:
"你在合并后并没有得到3个完全相等的列,而是得到了一个包含列表和一个普通值的元组,就像你所贴出的那样:
Out[]: [(['2022', 4], 'Random1'), (['2023', 5], 'Random2')]
你希望正确地在这些列上建立索引,应该这样做:
sorted(c, key=lambda i:(i0, i1))
([['2022', 4], 'Random1'), (['2023', 5], 'Random2')]
你可能会尝试类似以下的方法:
key = lambda ((year, number), random_string): (number, random_string)
但实际上这在 PEP 3113 中已经被移除了。
或者,你可以使用列表理解来真正将列表合并到你想要的结果中,而不是使用普通的 'list' 调用:
[x + [y] for x, y in zip(a, b)]
[['2022', 4, 'Random1'], ['2023', 5, 'Random2']]
现在你的初始代码应该可以正常工作了。"
英文:
You're not exactly getting 3 equal columns after the merge, but rather a tuple of a list and a plain value as you posted:
Out[]: [(['2022', 4], 'Random1'), (['2023', 5], 'Random2')]
You want to properly index on the columns and it should work:
>>> sorted(c, key=lambda i:(i[0][1], i[1]))
[(['2022', 4], 'Random1'), (['2023', 5], 'Random2')]
You might be tempted to attempt something like
key = lambda ((year, number), random_string): (number, random_string)
but this was actually removed in PEP 3113
Alternatively you could truly merge the lists to the result you wanted with this list comprehension instead of the plain list
call:
>>> [x + [y] for x, y in zip(a, b)]
[['2022', 4, 'Random1'], ['2023', 5, 'Random2']]
Now your initial code should work fine.
答案2
得分: 1
"IndexError: tuple index out of range" 是因为您的列表 c 中的元素是元组,而您试图访问每个元组的第二个和第三个索引。但是,c 中的元组只有两个元素:来自 a 的第一个元素和来自 b 的相应元素。
修正后的答案可以是:
a = [['2022', 4], ['2023', 5]]
b = ["Random1", "Random2"]
c = list(zip(a, b))
c_sorted = sorted(c, key=lambda i: (i[0][1], i[1]))
print(c_sorted)
"i0" 访问了来自 a 的子列表 "i[0]" 的第二个元素(1)。这是您希望用作初始排序的值。"i1" 访问了来自 b 的相应元素。这是您希望用作次要排序标准的值。
英文:
"IndexError: tuple index out of range," occurs because the elements in your list c are tuples, and you are trying to access the second and third indices of each tuple. However, the tuples in c have only two elements: the first element from a and the corresponding element from b.
corrected answer can be:
a = [['2022', 4], ['2023', 5]]
b = ["Random1", "Random2"]
c = list(zip(a, b))
c_sorted = sorted(c, key=lambda i: (i[0][1], i[1]))
print(c_sorted)
i0 accesses the second element (1) of the sublist (i[0]) from a. This is the value you want to use for the initial sorting. i1 accesses the corresponding element from b. This is the value you want to use as the secondary sorting criterion.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论