gpt4 book ai didi

python - python中的dict() vs {}哪个更好?

转载 作者:IT老高 更新时间:2023-10-28 20:52:00 25 4
gpt4 key购买 nike

我想知道,在以下 2 种方法中声明字典的最佳做法是什么?为什么?

>>>a=dict(one=2, two=3)  # {"two":3, "one":2}
>>>a={"two":3, "one":2}

最佳答案

你相信someone has already analyzed that (从性能角度)。

With CPython 2.7, using dict() to create dictionaries takes up to 6times longer and involves more memory allocation operations than theliteral syntax. Use {} to create dictionaries, especially if you arepre-populating them, unless the literal syntax does not work for yourcase.

关于python - python中的dict() vs {}哪个更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17097985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com