Python--字典、赋值符号的辨析、复合数据结构

技术 · 2019-01-24 · 481 人浏览

字典

  • 结构--字典名={字:解释,…}
  • 查字典--print(字典名[字])
>>> names={'Tom':'Tom is very tall','William':'William is very handsome'}
>>> names
{'Tom': 'Tom is very tall', 'William': 'William is very handsome'}
>>> print(names['Tom'])
Tom is very tall

也可用.get()

>>> names
{'Tom': 'Tom is very tall', 'William': 'William is very handsome'}
>>> print(names.get('Tom'))
Tom is very tall
>>> print(names.get('Mary'))
None

.get()自定义返回值

>>> names
{'Tom': 'Tom is very tall', 'William': 'William is very handsome'}
>>> print(names.get('Mary','No here'))
No here

===is辨析

  • =--赋值,给数据添加一个地址
  • ==--对数据进行比较
  • is--对数据地址进行比较
>>> a=[1,2,3,4,5,6]
>>> b=a
>>> c=[1,2,3,4,5,6]
>>> print(a==b)
True
>>> print(a==c)
True
>>> print(a is b)
True
>>> print(a is c)
False

复合数据结构

>>> people={'Tom':{'height':175,'weight':'80kg','color':'blue'},'Mary':{'height':180,'weight':'85kg','color':'black'},'William':{'height':165,'weight':'50kg','color':'green'}}
>>> Tom=people['Tom']
>>> Tom_color=people['Tom']['color']
>>> print(Tom)
{'height': 175, 'weight': '80kg', 'color': 'blue'}
>>> print(Tom_color)
blue
python 字典 辨析 复合数据结构
  1. 膜拜学习下!

    1. 猫抓草 (作者)  2019-01-25
      @异星软件空间

      你可是软件资源大佬啊

Theme Jasmine by Kent Liao