python - 如何在字典Python中添加列表作为值
问题描述
我正在尝试创建一个字典,其中键是字符串,值是列表。类似的东西
l1 = ['a', 'b', 'c']
我想在 for 循环的每次迭代中添加类似于该列表的列表。
我试过这个
dicc['first'].append(l1)
出口应该是这样的:
dicc={'first': ['a', 'b', 'c']}
我总是遇到同样的错误:list indices must be integers or slices, not str
我该怎么做?
解决方案
在这种情况下,您必须将对象分配给字典中的键的列表,在您的情况下:
dicc['first'] = l1
推荐阅读
- amazon-web-services - 具有 AssociateAddress 权限但不允许重新关联弹性 IP 的 IAM 用户策略选项
- sql - 具有内部联接的 SQL Server 返回的结果比预期的要多
- c# - 切换按钮作为显示隐藏
- javascript - ajax 响应返回 html 响应(XSS 验证码)
- ios - 扩展方法不起作用:“UIImage”类型的值没有成员“压缩”
- asp.net - ASP.NET 集成测试 - TestHost 总是返回 500 内部服务器错误
- bash - 通过查看查找文件中列出的 md5 校验和来删除文件
- html - VBA点击按钮
- django - Django - 是否可以只翻译一些模型(管理表单)?
- python - 绘制一个包含所有 1 和 0 的数据帧,其中 x 代替 1,空白为 0