将新值附加到字典键给出 AttributeError: 'int' object has no attribute 'append'

克鲁什纳纳夫勒

我正在尝试使用 enumerate() 函数将列表映射到字典中,但是当我尝试将新值附加到键时,带有附加的值对会给出 AttributeError。根据我的理解,我认为整数不接受附加函数,但我没有附加到整数,那么为什么我会收到这样的错误。这是我的代码:

movies = ['star wars', 'avenger', 'iron man', 'spider man', 'star wars', 'spider man', 'iron man', 'star wars', 'star wars']
schedule = {}

for day, movie in enumerate(movies):
  if movie not in schedule:
    schedule[movie] = day
  else:
    schedule[movie].append(day)
print(schedule)

如果有人知道如何处理它,请告诉我。

光明

append方法是为列表定义的,但您永远不会在代码中创建列表。的值schedule[movie]设置为day,它是一个整数。以后不能附加到该值!

我怀疑您希望schedule始终拥有列表值。在这种情况下,您需要使用包含第一天的列表进行初始分配,而不是直接使用日期值:

if movie not in schedule:
    schedule[movie] = [day]      # add brackets to make a list
else:
    schedule[movie].append(day)  # now this will work

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python 类问题“AttributeError: 'int' object has no attribute isSolvable”

为什么我的代码不起作用 AttributeError: 'int' object has no attribute 'isdigit'

调用返回哈希值的函数:AttributeError: 'tuple' object has no attribute 'encode'

试图将 model.pickle 加载到 API 但引发 ```AttributeError: 'Data' object has no attribute 'columns'```

命令引发异常:AttributeError: 'str' object has no attribute 'id'

我收到此错误 --> AttributeError: 'NoneType' object has no attribute 'predict'

Biopython SeqIO: AttributeError: 'str' object has no attribute 'id'

尝试调用实例方法输出 AttributeError: object has no attribute

如何解决 AttributeError: 'Resource' object has no attribute in Google API?

命令引发异常:AttributeError: 'NoneType' object has no attribute 'send'

命令引发异常:AttributeError: 'TextChannel' object has no attribute 'message'

命令引发异常:AttributeError: 'NoneType' object has no attribute 'name'

为什么我会收到此错误 AttributeError: 'str' object has no attribute 'get' where get 函数在字典上?

將圖像添加到 matplotlib 中的圖例返回錯誤:AttributeError: 'BarContainer' object has no attribute '_transform'

将 CountVectorizer 与 Pipeline 和 ColumnTransformer 一起使用并获取 AttributeError: 'numpy.ndarray' object has no attribute 'lower'

使用 zip 将列表与数据框结合起来 - AttributeError: 'str' object has no attribute 'loc'

使用 UDF 时如何解决 AttributeError: 'RDD' object has no attribute '_get_object_id'?

如何访问列表成员的属性 AKA AttributeError: 'list' object has no attribute 'name'

为什么我会收到错误 AttributeError: 'Response' object has no attribute 'type'?

我如何解决错误消息 AttributeError: type object 'Player' has no attribute 'rect'?

with file.open('r',encoding="utf-8") as f: AttributeError: 'str' object has no attribute 'open'

在以下代码中得到 AttributeError: 'FarmerClass' object has no attribute 'farmer_name'

从另一个文件调用方法 AttributeError: object has no attribute

官方 ZeroOut 渐变示例错误:AttributeError: 'list' object has no attribute 'eval'

无法在 python 中加载 json 对象,获取 AttributeError: 'Response' object has no attribute 'loads'

Keras,K.repeat_elements 导致 AttributeError:'Tensor' object has no attribute '_keras_history'

Dask read_sql_table 抛出错误 AttributeError: 'Connection' object has no attribute '_instantiate_plugins'

为什么这段代码会产生错误“AttributeError: 'Match' object has no attribute 'replace'”?

根据日期条件创建列,但出现此错误 AttributeError: 'SeriesGroupBy' object has no attribute 'sub'?