List(你可以中譯為串列,上課中會說List)。顧名思義,「a list of something」,通常代表的是「something arranged in order」。也就是把一群物件、東西、數值、變數照順序排好。List就是這樣的結構。只要放進List中,他就會被給予一個整數編號,而整數編號從0開始編起。在下列程式=(assignment)右側為List([]是一個空的List),當其被assign給左側時,左側的變項就是List型態。
alist =[10,9,8,7,6,5,4,3,2,1]# print whole listprint(alist)# print the last one elementprint(alist[-1])# print the first and second elementsprint(alist[1])print(alist[2])# print the length of the list by function len()print(len(alist))# length of
Dictionary(你可以中譯為「字典」,但上課會直接稱之為dict)如同其字面上的意思,是給人就單字查詢意義的。在python中也不例外,他所存放的是「key to value pairs」。所以實際上他的內容並沒有順序,所以他不像list是用index 0, 1, 2...來存取。他是用key來存取value。在下面的例子中adict[1]所對應的值就是3,2所對應的值就是4。
# print the 1 to 2 elements
print(alist[1:3])
# print the last 3 to 2 elements from the end
print(alist[-3:-1])
[9, 8]
[3, 2]
# Append two integers to alist one by one
alist.append(3)
alist.append(4)
# Extend alist with another list [3, 4, 5]
alist.extend([3, 4, 5])
# Append alist with another list [3, 4, 5]
alist.append([3, 4, 5])
# print out alist to see what happens
print(alist)
print([1, 2, 3] + [4, 5, 6])
alist = [1, 2, 3, 4, 'abc', '123', '123.1', [1, 2, 3] ]
print(len(alist)) #length len()
print(alist[7], type(alist[7]))
# Access any elements in the second layers of alsit
print(alist[7][1], type(alist[7][1])) # index
# access the dictionary
bdict = {1:3, '1':3, 4:[2, 4]} # initialization
# print whole dict
print(bdict)
# print the value of key '1'
print(bdict['1'])
# print the value of key 1
print(bdict[1])
# print the first element of the list mapped by key 4
print(bdict[4][0])
{1: 3, '1': 3, 4: [2, 4]}
3
3
2
adict = {}
adict[3] = "hahaha"
for i in range(10):
adict[i] = i*10
print(adict)
# import requests library to send web query
import requests
# import json library to parse json format
import json
response = requests.get('https://tcgbusfs.blob.core.windows.net/blobyoubike/YouBikeTP.gz')
print(type(response)) # <class 'requests.models.Response'>
print(type(response.text)) # <class 'str'>
data = json.loads(response.text)
# print the type of whole data
print(type(data))
<class 'dict'>
# print the type of whole data
print(type(data))
# print all key of the whole data
print(data.keys())
<class 'dict'>
dict_keys(['retCode', 'retVal'])
# print the type of whole data
print(type(data))
# print all key of the whole data
print(data.keys())
# traversing the data
print(data['retVal']['0137'])
# print the type of whole data
print(type(data))
# print all key of the whole data
print(data.keys())
# traversing the data
print(data['retVal']['0137'])
print(data['retVal']['0137']['sbi'])
print(data['retVal']['0137']['tot'])
for k in data["retVal"].keys():
print(data["retVal"][k]["sbi"])
for k in data["retVal"]:
print(data["retVal"][k]["sbi"])
for k in data["retVal"].items():
print(k[1]["sbi"])
for k, v in data['retVal'].items():
print(k, v["sbi"])
area_dict = {}
for key, row in data["retVal"].items():
if row["sarea"] not in area_dict:
area_dict[row["sarea"]] = 1
else:
area_dict[row["sarea"]] += 1
area_dict