高效创建多个词典
我必须创建10多个词典。有没有更有效的方法来使用Python的内置库创建多个字典,如下所述:
dict1_1= {
"value":100,
"secondvalue":200,
"thirdvalue":300
}
dict1_2= {
"fixedvalue":290,
"changedvalue":180,
"novalue":0
}
I have to create more than 10 dictionaries. Is there a more efficient way to create multiple dictionaries using Python's built-in libraries as described below:
dict1_1= {
"value":100,
"secondvalue":200,
"thirdvalue":300
}
dict1_2= {
"fixedvalue":290,
"changedvalue":180,
"novalue":0
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
dict 内置函数将从关键字参数创建一个字典:
但是你可以使用整数作为关键字参数:
但是,
dict
也将接受可迭代的键/值元组,在这种情况下,它们的键可能是整数如果所有字典的键都相同,则可以使用 zip:
但是,如果您的密钥不一致,使用也没有问题文字
{...}
构造函数。事实上,如果您想要效率,文字构造函数可能是最佳选择 。The dict builtin will create a dictionary from keyword arguments:
but you can use integers as keyword arguments:
However,
dict
will also accept an iterable of key/value tuples, and in this case they keys may be integersIf your keys are the same for all dicts you can use zip:
However, if your keys are not consistent, there's nothing wrong with using the literal
{...}
constructor. In fact, if it's efficiency that you want, the literal constructor may be the best choice.您可以使用一个简单的函数来创建新词典。看下面的代码:
you can use a simple function to create new dictionaries. Look at the code below: