5 请教各位,'float' object is not subscriptable 的问题出在哪里啊

各位大侠,问题如下

  1. list_001 = [9.1454247, 53.48538079999999, 9.01548, 53.16778, 9.1434819, 53.4873555, 9.1451686, 53.48564990000001, 9.14256, 53.48726, 9.1393, 53.48882, 9.145203, 53.4865261, 9.1449977, 53.4899644, 9.1448787, 53.4857361, 9.14495, 53.48965, 9.145296799999999, 53.485342, 9.1447931, 53.4865513, 9.1454247, 53.48538079999999]

  2. for num1 in range(1, len(list_001)):
  3.   k1 = list_001[num1][0]
  4.   k2 = list_001[num1][1]
  5.   print(num1)
  6.   print(k1)
  7.   print(k2)
复制代码

运行结果报错是

  File "C:\Python_3.6\Python_File_Py\EX_001\range_print.py", line 4, in <module>
    k1 = list_001[num1][0]
TypeError: 'float' object is not subscriptable

请教大家应该怎么改正啊



请先 登录 后评论

3 个回答

泡泡龙
num1在这里是1~25,如果要遍历list_001 直接for num1 in list_001就可以了。如果是要访问第N个元素list_001[N]
请先 登录 后评论
李宗县
for num1 in range(len(list_001)):
  k1 = list_001[num1]
  k2 = list_001[num1+1]
  print(num1)
  print(k1)
  print(k2)
请先 登录 后评论
时榕-余


不知道你想要輸出哪裡一種結果:
以下兩種,有可能其中一種是你要的

  1. list_001 = [9.1454247, 53.48538079999999, 9.01548, 53.16778, 9.1434819, 53.4873555, 9.1451686, 53.48564990000001, 9.14256, 53.48726, 9.1393, 53.48882, 9.145203, 53.4865261, 9.1449977, 53.4899644, 9.1448787, 53.4857361, 9.14495, 53.48965, 9.145296799999999, 53.485342, 9.1447931, 53.4865513, 9.1454247, 53.48538079999999]

  2. for i, num1 in enumerate(list_001):
  3.     k1 = list_001[i]
  4.     k2 = list_001[i+1]
  5.     print(num1)
  6.     print(k1)
  7.     print(k2)
复制代码

  1. list_001 = [9.1454247, 53.48538079999999, 9.01548, 53.16778, 9.1434819, 53.4873555, 9.1451686, 53.48564990000001, 9.14256, 53.48726, 9.1393, 53.48882, 9.145203, 53.4865261, 9.1449977, 53.4899644, 9.1448787, 53.4857361, 9.14495, 53.48965, 9.145296799999999, 53.485342, 9.1447931, 53.4865513, 9.1454247, 53.48538079999999]

  2. for i in range(0, len(list_001), 2):
  3.     k1 = list_001[i]
  4.     k2 = list_001[i+1]
  5.     print(i)
  6.     print(k1)
  7.     print(k2)
复制代码
请先 登录 后评论
  • 3 关注
  • 0 收藏,310 浏览
  • 千四儿 提出于 2021-09-11 21:16

相似问题