欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

python字典和結構化數據

系統 1609 0

5.1? 字典數據類型

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

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

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

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

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

red 42
PYthon學習企鵝裙:88198-2657  領取python自動化編程資料教程
            
          

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

            
              >>> list(spam.keys())
['color', 'age'] >>> list(spam.values())
['red', 42] >>> spam
{'color': 'red', 'age': 42}
PYthon學習企鵝裙:88198-2657  領取python自動化編程資料教程
            
          

5.1.2 get()方法setdefault()方法

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

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

如果程序中導入了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))這兩種表達式等價
            
          

運行結果:

            {' ': 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)建一個好玩的視頻游戲。用于對玩家物品清單建模的數據結構是一個字典。其中鍵是字符串,描述清單中的物品,值是一個整型值,說明玩家有多少該物品。例如,字典值{'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1,'arrow': 12}意味著玩家有 1 條繩索、6 個火把、42 枚金幣等。 寫一個名為displayInventory()的函數,它接受任何可能的物品清單,并顯示如下:

            
              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. 假設征服一條龍的戰(zhàn)利品表示為這樣的字符串列表:
            
              dragonLoot = ['gold coin', 'digger', 'gold coin', 'gold coin', 'ruby']
            
          

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

            
              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()函數)將輸出如下:

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

?


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 成人 在线 | 玖玖精品| 香港一级毛片在线播放 | 欧美无乱码久久久免费午夜一区 | 欧美成视频在线观看 | 九九香蕉视频 | 亚洲精品国产电影 | 日韩手机专区 | 久草在线精品ac | 狠狠色丁香婷婷综合 | 日本黄色一级片视频 | 一区二区三区久久 | 国产乱码视频 | 精品久久久中文字幕一区 | 欧美成视频无需播放器 | 欧美国产一区二区 | 玖玖在线精品 | 精品一区二区三区在线观看 | 国产98在线传媒在线视频 | 5月婷婷6月丁香 | 精品国产色 | 日本中文字幕在线观看 | 富二代视频污 | 久久国产精品一区二区 | 国产激情久久久久久熟女老人AV | 亚洲综合视频在线观看 | 国精品一区 | 日本大学生免费一级一片 | 高清在线不卡 | 午夜免费视频 | 中文字幕国产日韩 | 成人精品免费视频 | 亚洲午夜久久久精品影院 | 狠狠狠狠狠狠狠狠狠狠 | 成人黄色在线视频 | 国产浮力影院在线地址 | 男人午夜免费视频 | 福利视频二区 | 精品热99 | 免费人成又黄又爽的视频强 | 日韩精品久久久久 |