简体   繁体   English

“TypeError:列表索引必须是整数或切片,而不是 str”

[英]"TypeError: list indices must be integers or slices, not str"

Trying to complete a data analysis homework asignment, but constantly getting the same error despite several attempts试图完成一项数据分析作业,但多次尝试后仍不断出现相同的错误

Some help would be greatly appreciated.一些帮助将不胜感激。 The following is my code:以下是我的代码:

def is_valid_duration(duration_as_string):
try:
    duration = str(duration_as_string)
except ValueError:
    return False
else:
    duration = duration.strip("''")
    duration = duration.strip("`")
    return float(duration) > 10

filepath = "ufo-sightings.csv"


sightings_us = [row for row in ufosightings_count if row["country"] == "us" ]
fball = [row for row in sightings_us if (is_valid_duration(row["duration (seconds)"])) and (row["shape"] == "fireball")]

print(fball["datetime"], fball["state"])

the error I'm getting is this:我得到的错误是:

TypeError Traceback (most recent call last) in 28 fball = [row for row in sightings_us if (is_valid_duration(row["duration (seconds)"])) and (row["shape"] == "fireball")] 29 ---> 30 print(fball["datetime"]) 28 fball = [row for row in sightings_us if (is_valid_duration(row["duration (seconds)"])) 和 (row["shape"] == "fireball")] 中的 TypeError Traceback (最近一次调用最后一次)] 29 - --> 30 打印(fball["datetime"])

TypeError: list indices must be integers or slices, not str TypeError:列表索引必须是整数或切片,而不是 str

I guess row is some kind of dictionary with strings as keys.我猜 row 是某种以字符串为键的字典。 fball is a list of rows (not a dictionary as row), so fball needs an integer or slice as index, not a string. fball 是行列表(不是作为行的字典),因此 fball 需要 integer 或切片作为索引,而不是字符串。 So you can do所以你可以做

for row in fball:
   print(row["datetime"], row["state"])

or或者

for ix in range(len(fball)):
  print(fball[ix]["datetime"],fball[ix]["state"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “TypeError:list indices必须是整数或切片,而不是str” - “TypeError: list indices must be integers or slices, not str” TypeError:列表索引必须是整数或切片,而不是 str - TypeError: List indices must be integers or slices and not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str TypeError:列表索引必须是整数或切片而不是 str - TypeError: list indices must be integers or slices not str Python3-TypeError:列表索引必须是整数或切片,而不是str-List - Python3 - TypeError: list indices must be integers or slices, not str - List 使用 Python TypeError 解析 JSON:列表索引必须是整数或切片,而不是 str - Parsing JSON with Python TypeError: list indices must be integers or slices, not str 如何解决“类型错误:列表索引必须是整数或切片,而不是 str” - How to solve "TypeError: list indices must be integers or slices, not str" Scrapy TypeError:列表索引必须是整数或切片,而不是 str - Scrapy TypeError: list indices must be integers or slices, not str 遍历字典:类型错误:列表索引必须是整数或切片,而不是 str - Iterating through a dictionary: TypeError: list indices must be integers or slices, not str 类型错误:列表索引必须是整数或切片,而不是 BioPython 中的 str - TypeError: list indices must be integers or slices, not str in BioPython
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM