简体   繁体   English

如何在字典Python中添加列表作为值

[英]How to add a list as value in dictionary Python

I'm trying to create a dictionary where keys are strings and values are lists. 我正在尝试创建一个字典,其中键是字符串,值是列表。 Something like that 像这样

l1 = ['a', 'b', 'c']

and I want to add lists similar to that one in every iteration in a for loop. 我想在for循环的每次迭代中添加与该列表相似的列表。

I've tried this 我已经试过了

dicc['first'].append(l1)

The exit should be something like that: 出口应该是这样的:

dicc={'first': ['a', 'b', 'c']}

I always get the same error: list indices must be integers or slices, not str 我总是遇到相同的错误: list indices must be integers or slices, not str

How can i do it? 我该怎么做?

在这种情况下,您必须为字典中的键分配一个列表给您,在这种情况下:

dicc['first'] = l1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM