python中字典的几种定义方式
Posted David-lcw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中字典的几种定义方式相关的知识,希望对你有一定的参考价值。
# 方式1 >>> d = dict(name=‘Bob‘, age=20, score=88) >>> print(d) {‘name‘: ‘Bob‘, ‘age‘: 20, ‘score‘: 88} >>> d = dict("name"=‘Bob‘, "age"=20, "score"=88) SyntaxError: keyword can‘t be an expression >>>d = dict("name":‘Bob‘, "age":20, "score":88) SyntaxError: invalid syntax # 方式2 >>> d = {"name":‘Bob‘, "age":20, "score":88} >>> print(d) {‘name‘: ‘Bob‘, ‘age‘: 20, ‘score‘: 88} >>> d = {"name"=‘Bob‘, "age"=20, "score"=88} SyntaxError: invalid syntax >>> d = {name:‘Bob‘, age:20, score:88} Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> d = {name:‘Bob‘, age:20, score:88} NameError: name ‘name‘ is not defined
以上是关于python中字典的几种定义方式的主要内容,如果未能解决你的问题,请参考以下文章