【问题标题】:TypeError: 'int' object is not subscriptable pythonTypeError:'int'对象不可下标python
【发布时间】:2023-03-20 11:15:02
【问题描述】:

我正在尝试使用 CSV 文件返回一系列口袋妖怪的编号,使用最小值和最大值,所以我想将数字更改为整数。到目前为止,如果我不将输入作为整数,我只能返回一个精确的口袋妖怪。我的 CSV 有 801 行长,poke[0] 是他们的编号,poke[1] 是他们的名字。

def number(list):
  newlist = []
  x = 0
  q2 = int(input("Minimum number? "))
  q3 = int(input("Maximum number? "))
  for poke in range(1,800):
    poke[0] = int(poke[0])
  for poke in list:
    if poke[0] >= q2 and poke[0] <= q3:
      newlist.append(poke[0] + " " + poke[1])
  for poke in newlist:
    print(newlist[x])
    x = x + 1

【问题讨论】:

  • 从 csv 文件读取的代码在哪里?
  • 仅供参考,您不应该在 Python 中直接命名 list,因为这是一个内置的保留字。更多信息:stackoverflow.com/questions/9109333/…

标签: python list csv integer


【解决方案1】:

在第一个循环中,您将 poke 递增为 1、800 范围内的整数。这个整数不能被索引。

【讨论】:

  • 那我怎样才能把它变成一个整数呢?
  • 没关系,我想通了。我只是将 CSV 上第一项上的 poke[0] 更改为 0,然后更改循环以适应整个列表。
  • for poke in list: poke[0] = int(poke[0]) for poke in list: if poke[0] &gt;= q2 and poke[0] &lt;= q3: a = str(poke[0]) + " " + str(poke[1]) newlist.append(a)
猜你喜欢
  • 2015-07-31
  • 2015-12-18
  • 2017-07-15
  • 2012-02-21
  • 2018-08-07
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多