日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解python字典和結(jié)構(gòu)化數(shù)據(jù)

5.1 字典數(shù)據(jù)類型

字典的索引可以使用許多不同類型的數(shù)據(jù),不只是整數(shù)。字典的索引被稱為“鍵”,鍵及其關(guān)聯(lián)的值稱為“鍵—值”對,在代碼中,字典輸入時帶花括號{}。

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供犍為網(wǎng)站建設(shè)、犍為做網(wǎng)站、犍為網(wǎng)站設(shè)計、犍為網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、犍為企業(yè)網(wǎng)站模板建站服務(wù),十多年犍為做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

字典中的表項是不排序的,所以字典不能像列表那樣切片。

5.1.1 keys()、values()和items()方法

key()、values()和items()方法將返回類似于列表的值,分別對應(yīng)于字典的鍵、值和鍵-值對。這些方法返回的值不是真正的列表,他們不能被修改,沒有append()方法。但這些數(shù)據(jù)類型可以用于for循環(huán)。

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

red
42

可以通過list()方法將字典轉(zhuǎn)換為列表

>>> list(spam.keys())
['color', 'age']
>>> list(spam.values())
['red', 42]
>>> spam
{'color': 'red', 'age': 42}

5.1.2 get()方法setdefault()方法

get()方法有兩個參數(shù):要取得其值的鍵,以及如果該鍵不存在時,返回的備用值

setdefault()方法提供了一種方式,傳遞給該方法的第一個參數(shù),是要檢查的鍵,第二個參數(shù),是如果該鍵不存在時要設(shè)置的值。如果該鍵存在就返回鍵值。

如果程序中導(dǎo)入了pprint()模塊,就可以使用pprint()和pformat()打印字典。

import pprint
message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
count = {}

for character in message:
   count.setdefault(character, 0)
   count[character] = count[character] + 1

print(pprint.pformat(count))
#pprint.pprint(count)  print(pprint.pformat(count))這兩種表達(dá)式等價

運行結(jié)果:

{' ': 13,
',': 1,
'.': 1,
'A': 1,
'I': 1,
'a': 4,
'b': 1,
'c': 3,
'd': 3,
'e': 5,
'g': 2,
'h': 3,
'i': 6,
'k': 2,
'l': 3,
'n': 4,
'o': 2,
'p': 1,
'r': 5,
's': 3,
't': 6,
'w': 2,
'y': 1}

5.2 實踐項目

  1. 好玩游戲的物品清單

你在創(chuàng)建一個好玩的視頻游戲。用于對玩家物品清單建模的數(shù)據(jù)結(jié)構(gòu)是一個字典。其中鍵是字符串,描述清單中的物品,值是一個整型值,說明玩家有多少該物品。例如,字典值{‘rope’: 1, ‘torch’: 6, ‘gold coin’: 42, ‘dagger’: 1,’arrow’: 12}意味著玩家有 1 條繩索、6 個火把、42 枚金幣等。 寫一個名為displayInventory()的函數(shù),它接受任何可能的物品清單,并顯示如下:

Inventory:
1 rop
6 torch
42 gold coin
1 dagger
12 arrow
Total number of items :  62

代碼如下:

def displayInventory(dic):
   print('Inventory:')
   count = 0
   for k, v in dic.items():
       print(str(v) + ' ' + k)
       count = v+count
   print('Total number of items : ', count)


dicValue = {'rop': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
displayInventory(dicValue)
  1. 假設(shè)征服一條龍的戰(zhàn)利品表示為這樣的字符串列表:
dragonLoot = ['gold coin', 'digger', 'gold coin', 'gold coin', 'ruby']

寫一個名為 addToInventory(inventory, addedItems)的函數(shù),其中 inventory 參數(shù) 是一個字典,表示玩家的物品清單(像前面項目一樣),addedItems 參數(shù)是一個列表, 就像 dragonLoot。 addToInventory()函數(shù)應(yīng)該返回一個字典,表示更新過的物品清單。

def displayInventory(dic):
   print('Inventory:')
   count = 0
   for k, v in dic.items():
       print(str(v) + ' ' + k)
       count = v+count
   print('Total number of items : ', count)


def addToInventory(inventory, addeditems):
   for i in addeditems:
       if i in inventory.keys():
           inventory[i] += 1
       else:
           inventory.setdefault(i, 1)            
   return inventory


inv = {'gold coin':42, 'rope':1}
dragonLoot = ['gold coin', 'digger', 'gold coin', 'gold coin', 'ruby']
inv = addToInventory(inv,dragonLoot)
displayInventory(inv)

前面的程序(加上前一個項目中的 displayInventory()函數(shù))將輸出如下:

Inventory:
45 gold coin
1 rope
1 digger
1 ruby
Total number of items :  48

分享題目:詳解python字典和結(jié)構(gòu)化數(shù)據(jù)
鏈接URL:http://m.5511xx.com/article/cdjsepd.html