[英]TypeError: 'float' object is not subscriptable when trying to sort index in lists of lists
In python 3.0 I am currently trying to use my insertion sort algorithm to sort a list of lists I have by their 2nd element (1st index).在 python 3.0 中,我目前正在尝试使用我的插入排序算法按第二个元素(第一个索引)对我拥有的列表列表进行排序。 For example, I would like to sort a list like this according to the value of the 2nd element in descending order.
例如,我想根据第二个元素的值按降序对这样的列表进行排序。
pre-sort:预排序:
list = [ ["Name1", 10.0, 9.5, 8.6, 4.0],
["Name2", 14.5, 9.1, 4.2, 0.6],
["Name3", 9.9, 42.0, 9.1, 4.1],
.....]
post-sort:后排序:
list = [ ["Name2", 14.5, 9.1, 4.2, 0.6],
["Name1", 10.0, 9.5, 8.6, 4.0],
["Name3", 9.9, 42.0, 9.1, 4.1],
.....]
My current insertion sort method looks like this:我当前的插入排序方法如下所示:
def sort_points(a_list):
"""Sorts in descending order."""
for index in range(1, len(a_list)):
value = a_list[index][1]
pos = index - 1
while pos >= 0 and a_list[pos][1] < value:
a_list[pos + 1] = a_list[pos]
pos -= 1
a_list[pos + 1] = value
And when I try to run the code I am given the following error:当我尝试运行代码时,出现以下错误:
Traceback (most recent call last):回溯(最近一次通话最后):
sort_points(allstars_2021)
File "C:/Users/Michael/PycharmProjects/NBAstats/CompareStats.py", line 16, in sort_points
while pos >= 0 and a_list[pos][1] < value:
TypeError: 'float' object is not subscriptable
Not sure what I am doing wrong here because I adjusted my insertion_sort algorithm I have used previously with float values but never in the scenario of sorting in element within a list of a list.不知道我在这里做错了什么,因为我调整了我之前使用浮点值的插入排序算法,但从未在列表列表中的元素中排序的场景中。 Any help would be greatly appreciated, Thank you!
任何帮助将不胜感激,谢谢!
This is not a answer, just want to show you how I reproduce this.这不是答案,只是想向您展示我如何重现此问题。
In [4]: list = [ ["Name1", 10.0, 9.5, 8.6, 4.0],
...: ["Name2", 14.5, 9.1, 4.2, 0.6],
...: ["Name3", 9.9, 42.0, 9.1, 4.1],]
In [5]: def sort_points(a_list):
...: """Sorts in descending order."""
...: for index in range(1, len(a_list)):
...: value = a_list[index][1]
...: pos = index - 1
...: while pos >= 0 and a_list[pos][1] < value:
...: a_list[pos + 1] = a_list[pos]
...: pos -= 1
...: a_list[pos + 1] = value
...:
In [6]: sort_points(list)
In [7]: list
Out[7]: [14.5, ['Name1', 10.0, 9.5, 8.6, 4.0], 9.9]
In [8]: sort_points(list)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-5a1fa048546f> in <module>
----> 1 sort_points(list)
<ipython-input-5-fc8e50df198d> in sort_points(a_list)
4 value = a_list[index][1]
5 pos = index - 1
----> 6 while pos >= 0 and a_list[pos][1] < value:
7 a_list[pos + 1] = a_list[pos]
8 pos -= 1
TypeError: 'float' object is not subscriptable
if you can give exact input for your function, I can try to fix that.如果您可以为您的 function 提供准确的输入,我可以尝试解决这个问题。
In the first iteration, your pos == 0, and your a_list[pos][1] == value
so it never enters the loop.在第一次迭代中,您的
pos == 0, and your a_list[pos][1] == value
,因此它永远不会进入循环。
and at the end of the loop, you replace the 2nd index a_list[pos+1] that is a_list[2]
with a float value.并在循环结束时,将第二个索引
a_list[pos+1] that is a_list[2]
替换为浮点值。 a_list[pos+1] = value
now in the 2nd iteration, when your code reaches your while loop, it tries to access a_list[2][1]
, but you already replace a_list[2]
with a float value 14.0, and therefore it throws the error that you are trying to subscript a float value.现在在第二次迭代中,当您的代码到达您的 while 循环时,它会尝试访问
a_list[2][1]
,但您已经将a_list[2]
替换为浮点值 14.0,因此它会抛出您正在尝试的错误下标一个浮点值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.