Python自学教程 6 字典和结构化数据(零基础新手小白适用型~)

6.1 字典数据类型

6.1.1 字典与列表

字典是不排序的,不能够像列表那样切片
尽管字典是不排序的,但是可以用任意值作为键

6.1.2 keys()/values()和items()方法

values()方法 +for=循环迭代值
keys()方法 +for=循环迭代键
items()方法 +for=循环迭代键值对

>>> spam={'color':'red','age':42}
>>> for v in spam.values():
	print(v)

	
red
42
>>> for k in spam.keys():
	print(k)

	
color
age
>>> for k in spam.items():
	print(k)

('color', 'red')
('age', 42)

如果想得到列表,可以采用list函数

>>> spam={'color':'red','age':42}
>>> for k,v in spam.items():
	print('Key:'+ k +'Value:'+ str(v))
Key:age Value:42
Key:color Value:red

在上面的代码中,for循环迭代字典中的每个值;for循环也可以迭代每个键,或者键值对。

6.1.3 检查字典中是否存在键或者值

在列表中,可以使用in or not in 检查值是否存在于列表中

>>> spam={'name':'Zok','age':7}
>>> 'name' in spam.keys()
True
>>> 'color' not in spam.keys()
True
>>> 'color' in spam
False

in spam相当于in spam.keys()是一个简写版本

6.1.4 get()方法

get()方法有两个参数,要取得其值的键,以及如果该键不存在时,返回的备用值。

>>> picnicitems={'apples':5,'cups':2}
>>> 'i am bringing '+str(picnicitems.get('cups',0))+'cups.'
'i am bringing 2cups.'
>>> 'i am bringing '+str(picnicitems.get('eggs',0))+'cups.'
'i am bringing 0cups.'
>>>  'i am bringing '+str(picnicitems.get('eggs',0))+'eggs.'
 

6.1.5 setdefault()方法

setdefault()方法用于设置当字典中缺少键时的默认值,当第二次调用时,其值还是第一次设置的值。

>>> spam={'name':'POOka','age':5}
>>> spam.setdefault('color','black')
'black'
>>> spam={'color':'black','age':5,'name':'POOka'}
>>> spam.setdefault('color','white')
'black'
>>> spam
{'color': 'black', 'age': 5, 'name': 'POOka'}

计算字符串的小程序

message='It was a bright cold day in april, and the clocks were strking thirteen.'
count={}
for character in message:
    count.setdefault(character,0)
    count[character]=count[character]+1
    print(count)
    
  {' ': 13, ',': 1, '.': 1, 'I': 1, 'a': 5, 'c': 3, 'b': 1, 'e': 5, 'd': 3, 'g': 2, 'i': 5, 'h': 3, 'k': 2, 'l': 3, 'o': 2, 'n': 4, 'p': 1, 's': 3, 'r': 5, 't': 6, 'w': 2, 'y': 1}

5.2 漂亮打印

为了使得输出结果更加干净,我们采用pprint函数或者pformat函数

import pprint
message='It was a bright cold day in April, and the clocks were striking.'
count={}
for character in message:
    count.setdefault(character,0)
    count[character]=count[character]+1
    pprint.pprint(count)
    
    {' ': 12,
 ',': 1,
 '.': 1,
 'A': 1,
 'I': 1,
 'a': 4,
 'b': 1,
 'c': 3,
 'd': 3,
 'e': 3,
 'g': 2,
 'h': 2,
 'i': 5,
 'k': 2,
 'l': 3,
 'n': 3,
 'o': 2,
 'p': 1,
 'r': 4,
 's': 3,
 't': 4,
 'w': 2,
 'y': 1}

如果字典本身包含嵌套的列表或字典,则pprint.pprint()函数就特别有用。

5.3 使用数据结构对真实世界建模

5.3.1 井字键盘

printBoard()函数可以处理任何井字棋数据结构

theBoard={'top.L':' ','top.M':' ','top.R':' ',
           'mid.L':' ','mid.M':' ','mid.R':' ',
          'low.L':' ','low.M':' ','low.R':' '}
def printBoard(board):
    print(board['top.L']+'|'+board['top.M']+'|'+board['top.R'])
    print('-+-+-')
    print(board['mid.L']+'|'+board['mid.M']+'|'+board['mid.R'])
    print('-+-+-')
    print(board['low.L']+'|'+board['low.M']+'|'+board['low.R'])
    print('-+-+-')
printBoard(theBoard)

运行该程序,棋盘能够打印在屏幕上

 | | 
-+-+-
 | | 
-+-+-
 | | 
-+-+-

5.3.2 嵌套的字典和列表

totalbrought()函数可以可以计算总数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值