首页 > 解决方案 > 如何从像列表这样的整数访问元素?

问题描述

我尝试运行代码:

for c in range(10, 21):
    print(c)
    print([c[1]])

我得到错误: TypeError: 'int' object is not callable 为什么?如何从整数中选择特定元素?

标签: pythoninteger

解决方案


result = [i for i in range(10,21)]
for c in result:
    print(c)
    print(result[1])

#你正在尝试的在 python 中是非法的。Int 'int' 对象不可订阅。#只有列表或元组、集合、字符串...是可下标的。


推荐阅读