#學習筆記Python#7、列表 字典(12下)&8、列表與字典 (第12章掃尾)&9、函式(13)
7、列表 字典(12下) 2017-08-09 19:14 查詢索引(為了找到一個元素位於列表中的什麼位置) 暫時沒有找到方法。。。。。
迴圈處理列表
letters=["a","b","d","e"]
for letter in letters:
print (letter)
結果:
a
b
d
e
這裡的迴圈變數是letter,每次迭代時,當前元素會儲存在變數letter中,然後顯示出來。
列表排序
letters=["a","f","d","b"]
print (letters)
letters.sort()
print(letters)
結果:
['a', 'f', 'd', 'b'] ['a', 'b', 'd', 'f']
sort()會自動按字母順序或者數字順序從小到大排序 且 sort()會原地修改列表,它會改變原始列表而不是建立一個新的有序列表。 因此 print(letters.sort())是錯誤的。 必須分兩步來完成:
letters.sort()
print(letters)
逆序排序
letters=["a","e","d","b"]
letters.reverse()
print (letters)
結果:
['b', 'd', 'e', 'a']
這個是把給定元素倒序展示。 另:
letters=["a","e","d","b"] print (letters) letters.sort(reverse=True) print(letters)
結果:
['a', 'e', 'd', 'b']
['e', 'd', 'b', 'a']
這個是按大小次序排好,即從大到小。
列表是可變數,數字與字串是不可改變的 元組------不可改變的列表 建立元組:
my_tuple=("red","green","blue")
使用圓括號 元組是不可改變的,所以無法對元組進行排序,也不能追加刪除,一旦建立,就一直保持不變。
————————————————————————————————————— 8、列表與字典 (第12章掃尾) 2017-08-12 21:52
雙重列表:
joemarks=[23,56,24,26] tommarks=[34,44,33,25] bethmarks=[35,56,45,44] classMarks=[[joemarks],[tommarks],[bethmarks]] print (classMarks)
結果:
[[23, 56, 24, 26], [34, 44, 33, 25], [35, 56, 45, 44]]
也可以直接 classMarks=[[23, 56, 24, 26], [34, 44, 33, 25], [35, 56, 45, 44]]
迴圈:
for stumarks in classMarks:
print (stumarks)
結果:
[23, 56, 24, 26]
[34, 44, 33, 25]
[35, 56, 45, 44]
從表中獲取一個值
print (classMarks[0][3])
0 1 2 3
23 56 24 26
34 44 33 25
35 56 45 44
行 可視為大的元素(列表的列表),列 為列表的列表元素
字典: 鍵 值 鍵值對的集合成為字典
建立一個空的字典 phonenumber={} 例:
phonenumber["jhon"]="123456"
print(phonenumber)
結果:{‘jhon’: ‘123456’}
也可以直接用phonenumber={'jhon': '123456'}
我們建立字典,可以在字典中查詢東西
phonenumber={}
phonenumber["jhon"]="123456"
phonenumber["jane"]="123478"
phonenumber["mary"]="129816"
print(phonenumber["jane"])
結果:123478
列表與字典: 相似: 包含任意型別 數字 字串 物件甚至其它集合的集合 都提供了在集合中查詢條目的方法 不同: 列表有順序,如果你按照某種順序新增元素,這些元素會保持這種順序。還可以排序 字典是無序的,你想字典中加入內容,打印出來順序可能會改變。 列表中的元素是索引訪問(用索引號)的,而字典中的條目是使用鍵來訪問的
keys
print(phonenumber.keys())
結果:dict_keys(['jhon', 'jane', 'mary'])
values
print(phonenumber.values())
結果:dict_values(['123456', '123478', '129816'])
雜湊表
字典中的值可以包含字典
字典中的鍵只可以使用不可變型別(布林,整數,浮點數,字串和元組)不能使用一個列表或者字典當做鍵,因為他們是可變型別
sorted() 字典是無序的,可以對鍵進行排序,按照鍵的順序列印
phonenumber["jhon"]="123456"
phonenumber["mary"]="129816"
phonenumber["jane"]="123478"
for key in sorted(phonenumber.keys()):
print(key,phonenumber[key])
結果:
jane 123478
jhon 123456
mary 129816
因為鍵的集合是個列表
對字典的值排序
phonenumber["jhon"]="923456"
phonenumber["mary"]="129816"
phonenumber["jane"]="143478"
for value in sorted(phonenumber.values()):
for key in phonenumber.keys():
if phonenumber[key]==value:
print(key,phonenumber[key])
結果:
mary 129816
jane 143478
jhon 923456
我們首先獲得排序之後,針對每一個值,迴圈遍歷字典中的所有鍵,直到找到與該值關聯的鍵。
del 刪除一個條目
del phonenumber["jane"]
print(phonenumber)
結果:{'jhon': '923456', 'mary': '129816'}
clear()刪除所有條目
phonenumber.clear()
print(phonenumber)
結果:{} in確定某個值在字典中是否存在
print("jane" in phonenumber)
結果True
————————————————————————————————————— 9、函式(13) 2017-08-15 14:56 使用def關鍵字定義一個函式,函式名後有一對括號,後面跟冒號。
def printMyAddress():
print("aaa")
print("www")
printMyAddress()
呼叫函式:實參;函式部分:形參 帶兩個引數的函式:
def printMyAddress(name,address):
print(name)
print(address)
printMyAddress("a","2")
帶返回值的函式:
def calculateTax(price,tax_rate):
total=price+(price*tax_rate)
return total
my_price=float(input("Enter a price:"))
totalPrice=calculateTax(my_price,0.06)
print("price=",my_price,"Total price=",totalPrice)
使用表示式的任何地方都可以使用函式來返回值,可以把返回值賦給一個變數,也可以在另一個表示式中使用,或者打印出來
變數作用域 對於函式而言,函式內的名字只是在函式執行時才會建立的。在函式執行之前,或者完成執行之後甚至根本不存在。 函式執行結束時,其中所有名字都不再存在。 程式中,使用或者可以使用變數的部分,稱為變數的作用域。
區域性變數:如price total,在函式以外的某個位置無法打印出來其值,只在函式執行時才存在。 全域性變數:函式以外定義的變數,有更大的作用區域,程式的主部分,
def calculateTax(price,tax_rate):
total=price+(price*tax_rate)
print(my_price)
return total
my_price=float(input("Enter a price:"))
totalPrice=calculateTax(my_price,0.06)
print("price=",my_price,"Total price=",totalPrice)
在函式內部可以打印出全域性變數的值。 如果試圖在函式內部改變全域性變數的值,你會得到一個新的區域性變數。
def calculateTax(price,tax_rate):
total=price+(price*tax_rate)
my_price=335555
print(my_price)
return total
my_price=float(input("Enter a price:"))
totalPrice=calculateTax(my_price,0.06)
print("price=",my_price,"Total price=",totalPrice)
結果:
Enter a price:4
335555
price= 4.0 Total price= 4.2
強制為全域性: 用關鍵字 global來做到,
def calculateTax(price,tax_rate):
global my_price
使用global關鍵字,Python不會建立名為my_price的區域性變數,而是會使用名為my_price的全域性變數,另外,如果還沒有名為my_price的全域性變數,Python會建立一個。
總: 區域性變數與全域性變數可以重名,最好不要,因為容易混亂只要有混亂,錯誤就趁機而入。 Python會在需要時自動建立新的區域性變數,或者也可以用global關鍵字阻止它建立。