40923248 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • 首YA
  • HW1
    • PCH 15 Introduction to Wireless Networking 無線網絡簡介
      • Wireless LANs 無線局域網
      • Wireless Devices 無線設備
      • Wireless Standards 無線標準
      • Privacy and Security 隱私和安全
      • Wireless Networking Types 無線網絡類型
    • PCH 16  Internet Connectivity 互聯網連接
      • Internet Connectivity 互聯網連接
      • Networking  Internet Connectivity  聯網 Internet連接
      • Setting up a Network 建立網絡
  • HW2
  • HW3
    • Birthday Dictionaries 生日字典
      • Exercise 33  and Solution   練習33 和解決方案
      • Discussion topics 討論主題
      • Dictionaries 辭典
      • QUICK REVIEW 快速復審
      • MORE ON DICTIONARY KEYS 有關字典鍵的更多信息
      • String formatting 字符串格式
      • Solutions 解決方案
    • Tic Tac Toe Draw 井字遊戲抽獎
      • Exercise 27 and Solution 練習27 和解決方案
      • Concepts 概念
      • Solutions解決方案
    • Birthday Plots 生日情節
      • Exercise 36 and Solution 練習36和解決方案
      • Discussion 討論區
      • When to make plots 什麼時候作圖
      • Plotting libraries in Python 用Python繪製庫
      • Installing bokeh 安裝背景虛化
      • Using bokeh 使用散景
  • 心得
  • 自評 65 分
Dictionaries 辭典 << Previous Next >> MORE ON DICTIONARY KEYS 有關字典鍵的更多信息

QUICK REVIEW 快速復審

The main topic for this week is the concept of a dictionary. In Python they are called dictionaries, but in other languages they may be called maps or hashmaps. The concept is the same: it is a way to use a key to access some value. It is mapping one set of data (the keys) to another set of data (the values). The only way to access the value part of the dictionary is to use the key.

As an example, grocery store systems can store the prices for various items by using a dictionary. In Python, it would look something like this:

本週的主題是字典的概念。在Python中,它們稱為字典,在其他語言中,它們可以稱為map或hashmaps。概念是相同的:這是一種使用鍵來獲取某些值的方法。它正在將一組數據(鍵)映射到另一組數據(值)。訪問字典的值部分的唯一方法是使用鍵。

例如,雜貨店系統可以使用字典來存儲各種商品的價格。在Python中,它看起來像這樣:

price_dictionary = {
	"banana": 1.50,
	"avocado": 0.99,
	"heirloom tomato": 0.89,
	"cherry tomato pack": 3.00
}

You specify the dictionary between the {}. Key / value pairs are specified as list (separated by commas). The key is on the left side, and the value is on the right side. Notice how I split up the dictionary definition on multiple lines - I did that just for convenience. You can also write the dictionary on the entire line, but it is hard to read (and it probably even goes off your browser window screen):

您可以在之間指定字典{}。鍵/值對被指定為列表(以逗號分隔)。在關鍵的是在左側,和值是在右側。請注意,我是如何在多行上拆分字典定義的-我這樣做只是為了方便。您還可以在整個行上編寫字典,但是很難閱讀(它甚至可能從您的瀏覽器窗口屏幕上消失):

price_dictionary = {
	"banana": 1.50, "avocado": 0.99, "heirloom tomato": 0.89, "cherry tomato pack": 3.00}

Now that we have the dictionary, we can query it (ask it) for the values associated with each key (for the prices associated with each food). We do that using brackets ([]). For example, to get the price of a banana:

現在我們有了字典,我們可以查詢(詢問)與每個鍵關聯的值(與每種食物關聯的價格)。我們使用方括號([])進行操作。例如,要獲取香蕉的價格:

>>> print(price_dictionary["banana"])
1.50

To get a list of all the keys, use the .keys() method:

要獲取所有鍵的列表,請使用.keys()方法:

>>> print(price_dictionary.keys())
["banana", "avocado", "heirloom tomato", "cherry tomato pack"]

We have created our dictionary with many items in it, but we can also add items to it one by one. It is almost like accessing an element in the dictionary, except instead of using the element, we assign something to it.

我們創建了包含很多項目的字典,但是我們也可以一個接一個地添加項目。就像訪問字典中的一個元素一樣,除了我們不使用該元素,而是為其分配了一些內容。

>>> price_dictionary["granny smith apple"] = 0.49
>>> price_dictionary["red delicious apple"] = 0.35
>>> print(price_dictionary["granny smith apple"])
0.49

Not all key / value pairs have to be the same. You can do something like:

並非所有鍵/值對都必須相同。您可以執行以下操作:

>>> price_dictionary["dog food"] = "only at Petco"
>>> print(price_dictionary["dog food"])
only at Petco

Of course, this can get messy quickly, so be careful!

當然,這會很快變得混亂,所以要小心!


Dictionaries 辭典 << Previous Next >> MORE ON DICTIONARY KEYS 有關字典鍵的更多信息

Copyright © All rights reserved | This template is made with by Colorlib