Python_7_Codecademy_7_Lists, For Loop and Dictionaries

<a href="http://www.lxweimin.com/p/54870e9541fc">總目錄</a>


課程頁面:https://www.codecademy.com/
內容包含課程筆記和自己的擴展折騰

Lists

  • 例如:list_sample = [1, 2, 3, "ZHANG Yong", "張雍"]
  • 和string一樣,可以是的:[]
  • 和string一樣,也有index,從0開始。
    print list_sample[4] Output: 張雍
  • 可以替換list里面的items,譬如把sample list里面的3換成"ZY":
    list_sample[2] = "ZY"
  • 增加 item: .append():
    list_sample.append(6) # [1, 2, 3, "ZHANG Yong", "張雍", 6]
  • 和string一樣,也有長度len()
  • 和string一樣,也能部分獲取: list_sample[n:m]
  • 詢問index: list.index(item) 譬如詢問"張雍"的index:
    print list_sample.index("張雍") Output: 4
  • 插入指定位置: list.insert(index, item)
list_sample = [1, 2, 3, "ZHANG Yong", "張雍"]
list_sample.insert(1, 1.5)
# 就變成了:[1, 1.5, 2, 3, "ZHANG Yong", "張雍"]
  • 排序list.sort()
#sort list_A in alphabetical order
list_A = ["cool", "astronaut", "zebra", "xenophobia", "recursive"]
list_A.sort()
print list_A

Output:
['astronaut', 'cool', 'recursive', 'xenophobia', 'zebra']

  • **移除item(s): ** list.remove(item)
list_A = ["cool", "astronaut", "zebra", "xenophobia", "recursive"]
list_A.remove("cool")
print list_A

Output:
['astronaut', 'zebra', 'xenophobia', 'recursive']

For loop

例0:String Looping
string也是可以loop的:

for x in "ZHANG_Yong"
    print x

Output:
Z
H
A
N
G
_
Y
o
n
g
Process finished with exit code 0

例1:打印出列表中都有哪些items:

list_sample = [1, 2, 3, "ZHANG Yong", "張雍"]
for x in list_sample:
  print "%s is in this list" % x

Output:
1 is in this list
2 is in this list
3 is in this list
ZHANG Yong is in this list
張雍 is in this list

例2:listA是數字,輸出其平方,且按照大小排序:

listA = [6, 7, 1, 9, 0]
listB = []
for x in listA:
  listB.append(x**2)
listB.sort()
print listB

Output:
[0, 1, 36, 49, 81]

例3:多個lists作為arguments

list_A = [3, 9, 17, 15, 19]
list_B = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
for a, b in zip(list_A, list_B):
    print max(a, b)
Console:
3
9
17
15
30

Dictionaries

  • 和string&list一樣,可以是的:{}
  • key-value pairs / tuples: 然后就能通過比較好記的key拿到對應的value
    {key : value, key2 : value2, ...}
d = {"birthday_day" : "1st", "birthday_month" : "June"}
print d["birthday_day"]

Output:
1st

  • 增加tuples: (和往list里面加items類似)
    dictionary[new_key] = new_value
d = {"birthday_day" : "1st", "birthday_month" : "June"}
d["fav_fruit"] = "orange"
print d

Output:
{'birthday_month': 'June', 'birthday_day': '1st', 'fav_fruit': 'orange'}

  • value可以是list:
d = {"birthday_day" : "1st", "birthday_month" : "June"}
d["fav_fruit"] = ["orange", "avocado", "banana"]
print d

Output:
{'birthday_month': 'June', 'birthday_day': '1st', 'fav_fruit': ['orange', 'avocado', 'banana']}

  • **len(d): **多少個tuples
  • 刪除tuple: del dictionary[key]
  • 更改value: `dictionary[key] = new_value
  • curly braces 之間可以斷行:
d = {
    'birthday_month': 'June', 
    'birthday_day': '1st', 
    'fav_fruit': ['orange', 'avocado', 'banana']
}
  • grab an item from a list in a dictionary
# grab my first two fav food
d = {
    'birthday_month': 'June', 
    'birthday_day': '1st', 
    'fav_fruit': ['orange', 'avocado', 'banana']
}
print d["fav_fruit"][:2]

Output:
['orange', 'avocado']

  • for loop and dictionary:
d = {
    'fav_fruit': ['orange', 'avocado', 'banana'],
    'birthday_month': 'June', 
    'birthday_day': '1st', 
}
# 注意上面dictionary里面tuples的順序
for x in d:
    print x + ":", d[x] 

Output:
birthday_month: June
birthday_day: 1st
fav_fruit: ['orange', 'avocado', 'banana']
注意這里的順序

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容