列表是咱們最之後最經常使用的數據類型之一,經過列表能夠對數據實現最方便的存儲、修改等操做git
定義列表api
1 cars = ["Camry","Accord","Teana"]
訪問列表中的元素,方式以下app
#列表第一個 >>>cars[0] Camry #列表最後一個 >>>cars[-1] Teana #列表第二個 >>>cars[1] Accord
切片取元素ide
1 >>> cars = ["Camry","Accord","Teana","Q50","ES","320i","S60L"] 2 #取第二到第三個元素 3 >>> cars[1:3] 4 ['Accord', 'Teana'] 5 #取第一到最後一個元素但不包含最後一個元素 6 >>> cars[0:-1] 7 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i'] 8 #取第一到最後一個元素 9 >>> cars[0:] 10 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i', 'S60L'] 11 #第一到最後一個元素,每隔三個取一個 12 >>> cars[0::3] 13 ['Camry', 'Q50', 'S60L']
添加this
1 >>> cars 2 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i', 'S60L'] 3 >>> cars.append("A3") 4 >>> cars 5 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i', 'S60L', 'A3']
插入編碼
1 >>> cars 2 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i', 'S60L', 'A3'] 3 >>> cars.insert(3,"Corolla") 4 >>> cars 5 ['Camry', 'Accord', 'Teana', 'Corolla', 'Q50', 'ES', '320i', 'S60L', 'A3']
修改spa
1 >>> cars 2 ['Camry', 'Accord', 'Teana', 'Corolla', 'Q50', 'ES', '320i', 'S60L', 'A3'] 3 >>> cars[-1] = "A4" 4 >>> cars 5 ['Camry', 'Accord', 'Teana', 'Corolla', 'Q50', 'ES', '320i', 'S60L', 'A4']
刪除code
1 >>> cars 2 ['Camry', 'Accord', 'Teana', 'Corolla', 'Q50', 'ES', '320i', 'S60L', 'A4'] 3 #刪除第四個元素 4 >>> del cars[3] 5 >>> cars 6 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', '320i', 'S60L', 'A4'] 7 #指定名稱刪除元素320i 8 >>> cars.remove("320i") 9 >>> cars 10 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L', 'A4'] 11 #彈出最後一個元素 12 >>> cars.pop() 13 'A4' 14 >>> cars 15 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L']
合併orm
1 >>> brands = ["Toyota","Nissan","Honda"] 2 >>> cars 3 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L'] 4 #將cars與brands合併 5 >>> cars.extend(brands) 6 >>> cars 7 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L', 'Toyota', 'Nissan', 'Honda'] 8 >>> brands 9 ['Toyota', 'Nissan', 'Honda']
拷貝blog
1 >>> cars 2 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L', 'Toyota', 'Nissan', 'Honda'] 3 >>> cars_copy = cars.copy() 4 >>> cars_copy 5 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L', 'Toyota', 'Nissan', 'Honda']
統計
1 >>> cars 2 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L', 'Toyota', 'Nissan', 'Honda'] 3 >>> cars.count("ES") 4 1
排序&逆序
1 >>> cars 2 ['Camry', 'Accord', 'Teana', 'Q50', 'ES', 'S60L'] 3 >>> cars.sort() 4 >>> cars 5 ['Accord', 'Camry', 'ES', 'Q50', 'S60L', 'Teana'] 6 >>> cars.reverse() 7 >>> cars 8 ['Teana', 'S60L', 'Q50', 'ES', 'Camry', 'Accord']
獲取下標
1 >>> cars 2 ['Teana', 'S60L', 'Q50', 'ES', 'Camry', 'Accord'] 3 >>> cars.index("ES") 4 3
元祖
元組與列表相似,也是存一組數,只是一旦被建立,便不能再修改,因此又叫只讀列表
1 >>> cars = ['Teana', 'S60L', 'Q50', 'ES', 'Camry', 'Accord']
它只有2個方法,一個是count,一個是index。
程序:購物車程序
需求:
1 product_list = [ 2 ("Iphone",5000), 3 ("Mac Pro",13000), 4 ("Iwatch",3000), 5 ("Bike",1000), 6 ("Book",80), 7 ("Coffee",30), 8 ] 9 shopping_list = [] 10 salary = input("input you salary:") 11 if salary.isdigit(): 12 salary = int(salary) 13 while True: 14 for item in product_list: 15 print(product_list.index(item),item) 16 user_choice = input("選擇要買啥?>>>:") 17 if user_choice.isdigit(): 18 user_choice = int(user_choice) 19 if user_choice < len(product_list) and user_choice >=0: 20 p_item = product_list[user_choice] 21 if p_item[1] <= salary: #買得起 22 shopping_list.append(p_item) 23 salary -= p_item[1] 24 print("added %s into shopping cart,your current balance is [%s]"% (item,salary)) 25 else: 26 print("你的餘額只剩[%s]啦,買不起啦"% (salary)) 27 else: 28 print("product code [%s] is not exist!"% user_choice) 29 elif user_choice == "q": 30 print("--------shopping list--------") 31 for p in shopping_list: 32 print(p) 33 print("your currnt balance:",salary) 34 exit() 35 else: 36 print("invalid option")
37 else:
38 print("invalid option")
1 name.capitalize() 首字母大寫 2 name.casefold() 大寫所有變小寫 3 name.center(50,"-") 輸出 '---------------------xxx----------------------' 4 name.count('x') 統計 x出現次數 5 name.encode() 將字符串編碼成bytes格式 6 name.endswith("x") 判斷字符串是否以x結尾 7 "xx\tx".expandtabs(10) 輸出'xx x', 將\t轉換成多長的空格 8 name.find('A') 查找A,找到返回其索引, 找不到返回-1 9 10 format : 11 >>> msg = "my name is {}, and age is {}" 12 >>> msg.format("xxx",26) 13 'my name is xxx, and age is 26' 14 >>> msg = "my name is {1}, and age is {0}" 15 >>> msg.format("xxx",26) 16 'my name is 26, and age is xxx' 17 >>> msg = "my name is {name}, and age is {age}" 18 >>> msg.format(age=26,name="xxx") 19 'my name is xxx, and age is 26' 20 format_map 21 >>> msg.format_map({'name':'xxx','age':26}) 22 'my name is xxx, and age is 26' 23 24 25 msg.index('x') 返回x所在字符串的索引 26 27 28 '9'.isdigit() 是否整數 29 name.isnumeric 30 name.isprintable 31 name.isspace 32 name.istitle 33 name.isupper 34 "|".join(['xxx','jack','rain']) 35 'xxx|jack|rain' 36 37 38 maketrans 39 >>> intab = "aeiou" #This is the string having actual characters. 40 >>> outtab = "12345" #This is the string having corresponding mapping character 41 >>> trantab = str.maketrans(intab, outtab) 42 >>> 43 >>> str = "this is string example....wow!!!" 44 >>> str.translate(trantab) 45 'th3s 3s str3ng 2x1mpl2....w4w!!!' 46 47 msg.swapcase 大小寫互換 48 49 50 >>> msg.zfill(40) 51 '00000my name is {name}, and age is {age}' 52 53 54 55 >>> n4.ljust(40,"-") 56 'Hello 2orld-----------------------------' 57 >>> n4.rjust(40,"-") 58 '-----------------------------Hello 2orld' 59 60 61 >>> b="ddefdsdff_哦" 62 >>> b.isidentifier() #檢測一段字符串能否被看成標誌符,便是否符合變量命名規則 63 True