【问题标题】:TypeError: 'float' object is not subscriptable when trying to sort index in lists of listsTypeError:'float'对象在尝试对列表列表中的索引进行排序时不可下标
【发布时间】:2021-06-16 07:24:12
【问题描述】:

在 python 3.0 中,我目前正在尝试使用我的插入排序算法按第二个元素(第一个索引)对我拥有的列表列表进行排序。 例如,我想按照第二个元素的值降序排列这样的列表。

预排序:

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],
        .....]

后排序:

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],
        .....]

我目前的插入排序方法是这样的:

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

当我尝试运行代码时,出现以下错误:

Traceback(最近一次调用最后一次):

  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

不确定我在这里做错了什么,因为我调整了我之前使用浮点值的插入排序算法,但从未在列表列表中对元素进行排序的场景中。任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 你能给出准确的输入列表吗?因为当我尝试使用您提供的列表时,它会返回另一个列表为[14.5, ['Name1', 10.0, 9.5, 8.6, 4.0], 9.9],以防万一,如果您有递归或尝试在同一个列表上运行相同的函数,您可能会遇到这个问题。

标签: python algorithm sorting software-design


【解决方案1】:

这不是答案,只是想告诉你我是如何重现这个的。

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

如果您可以为您的功能提供准确的输入,我可以尝试解决这个问题。

【讨论】:

    【解决方案2】:

    在第一次迭代中,您的 pos == 0, and your a_list[pos][1] == value 永远不会进入循环。

    在循环结束时,将第二个索引a_list[pos+1] that is a_list[2] 替换为浮点值。 a_list[pos+1] = value

    现在在第二次迭代中,当您的代码到达您的 while 循环时,它会尝试访问 a_list[2][1],但您已经将 a_list[2] 替换为浮点值 14.0,因此它会引发您尝试下标的错误一个浮点值。

    【讨论】:

      猜你喜欢
      • 2019-10-18
      • 2011-08-09
      • 2021-11-14
      • 2018-10-29
      • 1970-01-01
      • 2015-10-23
      • 2015-08-17
      • 2015-07-25
      • 2022-06-10
      相关资源
      最近更新 更多