笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册
查看: 7516|回复: 3

[问题求助] 请教各位,'float' object is not subscriptable 的问题出在哪里啊

[复制链接]

新手上路

Rank: 1

积分
10
发表于 2022-3-8 15:40:02 | 显示全部楼层 |阅读模式
  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

请教大家应该怎么改正啊
我知道答案 回答被采纳将会获得10 C币 已有3人回答
回复

使用道具 举报

新手上路

Rank: 1

积分
9
发表于 2022-3-8 15:40:24 | 显示全部楼层
  1. for num1 in range(len(list_001)):
  2.   k1 = list_001[num1]
  3.   k2 = list_001[num1+1]
  4.   print(num1)
  5.   print(k1)
  6.   print(k2)
复制代码
回复

使用道具 举报

新手上路

Rank: 1

积分
2
发表于 2022-3-8 15:40:59 | 显示全部楼层
不知道你想要輸出哪裡一種結果:
以下兩種,有可能其中一種是你要的


  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)
复制代码


回复

使用道具 举报

新手上路

Rank: 1

积分
9
发表于 2022-3-8 15:41:12 | 显示全部楼层
num1在这里是1~25,如果要遍历list_001 直接for num1 in list_001就可以了。如果是要访问第N个元素list_001[N]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-12-22 09:43 , Processed in 0.084274 second(s), 22 queries .

© 2001-2020

快速回复 返回顶部 返回列表