找回密碼
 立即註冊
查看: 18760|回復: 6

Dictionaries

[複製鏈接]

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

發表於 2016-10-12 16:50:10 | 顯示全部樓層 |閱讀模式
  1. # Assigning a dictionary with three key-value pairs to residents:
  2. residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}

  3. print residents['Puffin'] # Prints Puffin's room number
  4. print residents['Sloth']
  5. print residents['Burmese Python']
複製代碼


938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-12 17:38:41 | 顯示全部樓層
  1. menu = {} # Empty dictionary
  2. menu['Chicken Alfredo'] = 14.50 # Adding new key-value pair
  3. print menu['Chicken Alfredo']

  4. menu['Juice'] = 4.50
  5. menu['Rice'] = 4.50
  6. menu['Cake'] = 4.50

  7. print "There are " + str(len(menu)) + " items on the menu."
  8. print menu
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-12 17:43:52 | 顯示全部樓層
  1. # key - animal_name : value - location
  2. zoo_animals = { 'Unicorn' : 'Cotton Candy House',
  3. 'Sloth' : 'Rainforest Exhibit',
  4. 'Bengal Tiger' : 'Jungle House',
  5. 'Atlantic Puffin' : 'Arctic Exhibit',
  6. 'Rockhopper Penguin' : 'Arctic Exhibit'}
  7. # A dictionary (or list) declaration may break across multiple lines

  8. # Removing the 'Unicorn' entry. (Unicorns are incredibly expensive.)
  9. del zoo_animals['Unicorn']

  10. # Your code here!
  11. del zoo_animals['Sloth']
  12. del zoo_animals['Bengal Tiger']
  13. zoo_animals['Rockhopper Penguin'] = 'Taiwan'

  14. print zoo_animals
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2016-10-13 01:26:08 | 顯示全部樓層
  1. inventory = {
  2.     'gold' : 500,
  3.     'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
  4.     'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
  5. }

  6. # Adding a key 'burlap bag' and assigning a list to it
  7. inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']

  8. # Sorting the list found under the key 'pouch'
  9. inventory['pouch'].sort()

  10. # Your code here
  11. inventory['pocket'] = ['seashell', 'strange berry', 'lint']
  12. inventory['backpack'].sort()
  13. inventory['backpack'].remove('dagger')
  14. inventory['gold'] += 50
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2017-3-7 02:46:36 | 顯示全部樓層
  1. prices = {
  2.     "banana": 4,
  3.     "apple": 2,
  4.     "orange": 1.5,
  5.     "pear": 3
  6. }
複製代碼

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2017-3-9 14:29:37 | 顯示全部樓層
  1. prices = {
  2.     "banana" : 4,
  3.     "apple"  : 2,
  4.     "orange" : 1.5,
  5.     "pear"   : 3,
  6. }
  7. stock = {
  8.     "banana" : 6,
  9.     "apple"  : 0,
  10.     "orange" : 32,
  11.     "pear"   : 15,
  12. }

  13. total = 0

  14. for key in prices:
  15.     print key
  16.     print "price: %s" % prices[key]
  17.     print "stock: %s" % stock[key]
  18.     sub_total = prices[key] * stock[key]
  19.     print "sub total: %s" % sub_total
  20.     total = total + sub_total

  21. print total
複製代碼

用水果的價格及數量算出總金額

938

主題

506

回帖

3萬

積分

管理員

網頁設計師

積分
31361

最佳新人活躍會員熱心會員推廣達人宣傳達人灌水之王突出貢獻優秀版主榮譽管理論壇元老

 樓主| 發表於 2017-7-23 17:06:24 | 顯示全部樓層
  1. lloyd = {
  2.     "name": "Lloyd",
  3.     "homework": [90.0, 97.0, 75.0, 92.0],
  4.     "quizzes": [88.0, 40.0, 94.0],
  5.     "tests": [75.0, 90.0]
  6. }
  7. alice = {
  8.     "name": "Alice",
  9.     "homework": [100.0, 92.0, 98.0, 100.0],
  10.     "quizzes": [82.0, 83.0, 91.0],
  11.     "tests": [89.0, 97.0]
  12. }
  13. tyler = {
  14.     "name": "Tyler",
  15.     "homework": [0.0, 87.0, 75.0, 22.0],
  16.     "quizzes": [0.0, 75.0, 78.0],
  17.     "tests": [100.0, 100.0]
  18. }
  19. students = [lloyd, alice, tyler]

  20. for x in students:
  21.     print x["name"]
  22.    
  23. for y in students:
  24.     print y["homework"]
  25.    
  26. for z in students:
  27.     print z["quizzes"]
  28.    
  29. for o in students:
  30.     print o["tests"]
複製代碼
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|DD論壇 維護: Redd Design

GMT+8, 2024-9-20 00:12 , Processed in 0.042653 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回復 返回頂部 返回列表