萌新问 关于创建字典
我听甲鱼的课,总结了一下:1.直接写:
>>> dic1 = {1:'qq', 2:'wechat', 3:'ding'}
2.用dict函数,里面用元组/列表写一个映射:
>>> dic2 = dict(((1,'qq'),(2,'wechat'),(3,'ding')))
3.key+value的形式写里面的映射:
>>> dic3 = dict(a = 'qq', b = 'wechat', c = 'ding')
问题出在这里,就是我这个a,b,c一旦写成1,2,3就会报错:
>>> dic3 = dict(1 = 'qq', 2 = 'wechat', 3 = 'ding')
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
这是为什么呀?是因为key不能是数字吗?小甲鱼课上说key可以是整形呀~ dd 因为赋值表达式的被赋值对象不能是 值 >>> dic3 = dict(1 = 'qq', 2 = 'wechat', 3 = 'ding')
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
你在转变一下观点,变量的命名规则是什么?
>>> dic3 = dict('a' = 'qq', b = 'wechat', c = 'ding')
你把他写成这样也报错。
页:
[1]