names = ["old_driver","rain","jack","shanshan","peiqi","black_girl"] print(names)
names.insert(names.index("black_girl"),"alex") print(names)
names[names.index("shanshan")] = "姍姍" print(names)
names.insert(names.index("rain")+1,["oldboy","oldgirl"]) print(names)
print(names.index("peiqi"))
names.extend([1,2,3,4,2,5,6,2]) print(names)
print(names[4:7])
print(names[2:10:2])
print(names[-3:])
#枚舉 for index,name in enumerate(names): print("%s. %s" % (index,name))
#計數 index = 0 for name in names: print("%s. %s" % (index, name)) index += 1
for index,name in enumerate(names): if index % 2 == 0: names[index] = -1 print(names)
print(names.index(2,names.index(2)+1))
#!/usr/bin/env python # -*- coding:utf-8 -*- li = ["alec", " aric", "Alex", "Tony", "rain"] for S in li: S = S.strip() prefix_a = S.startswith('a') prefix_A = S.startswith('A') suffix_c = S.endswith('c') if prefix_a or prefix_A and suffix_c: print('要查找的元素:%s' % S) tu = ("alec", " aric", "Alex", "Tony", "rain") for S in tu: S = S.strip() prefix_a = S.startswith('a') prefix_A = S.startswith('A') suffix_c = S.endswith('c') if prefix_a or prefix_A and suffix_c: print('要查找的元素:%s' % S) dic = {'k1': "alex", 'k2': ' aric',"k3": "Alex", "k4": "Tony"} for S in dic.values(): S = S.strip() prefix_a = S.startswith('a') prefix_A = S.startswith('A') suffix_c = S.endswith('c') if prefix_a or prefix_A and suffix_c: print('要查找的元素:%s' % S)
li = ['alex','eric','rain'] v = len(li) print(v)
li = ['alex','eric','rain'] li.append('seven') print(li)
li = ['alex','eric','rain'] li.insert(0,'Tony') print(li)
li = ['alex','eric','rain'] li[1] = 'Kelly' print(li)
li = ['alex','eric','rain'] li.remove('eric') print(li)
li = ['alex','eric','rain'] v = li.pop(1) print(v,li)
li = ['alex','eric','rain'] li.pop(2) print(li)
li = ['alex','eric','rain','rain','rain'] del li[1:4] print(li)
li = ['alex','eric','rain'] li.reverse() print(li)
li = ['alex','eric','rain'] for i in range(len(li)): print(i)
li = ['alex','eric','rain'] for i,v in enumerate(li,100): print(i,v)
li = ['alex','eric','rain'] for i in li: print(i)
#!/usr/bin/python # -*- coding:utf-8 -*- li = ["hello", 'seven', ["mon", ["h", "kelly"], 'all'], 123, 446] print(li[2][1][1])
#!/usr/bin/python # -*- coding:utf-8 -*- li = ["hello", 'seven', ["mon", ["h", "kelly"], 'all'], 123, 446] li[2][2] = "ALL" print(li)
tu = ('alex','eric','rain') v = len(tu) print(v)
tu = ('alex','eric','rain') v = tu[1] print(v)
tu = ('alex','eric','rain') v = tu[0:2] print(v)
tu = ('alex','eric','rain') for i in tu: print(i)
for i in range(len(tu)): print(i)
tu = ('alex','eric','rain') for i,v in enumerate(tu,10): print(i,v)
元組的一級元素不可被修改增長刪除,有序,可迭代,可切片,可索引,可轉換爲列表。
不可被修改
tu = ("alex", [11, 22, {"k1": 'v1', "k2": ["age", "name"], "k3": (11,22,33)}, 44]) v = tu[1][2]["k2"] v.append("Seven") print(tu)
k3對應的是元組,不可修改
products = [ ['iphone8',6888], ['MacPro', 14800], ['小米6',2499], ['coffee',31],['book',80],['Nike shoes',799]]python
請打印出這樣的格式:
-----------商品信息 ------------
0. iphone8 6888
1. MacPro 14800
2. 小米6 2499
3. coffee 31
4. book 80
5. Nike shoes 799
products = [ ['iphone8',6888], ['MacPro', 14800], ['小米6',2499], ['coffee',31],['book',80],['Nike shoes',799]] print("商品信息".center(28,"-")) for index,product in enumerate(products): print("%s.%s %s" %(index,product[0],product[1])) # print("{0}.{1} {2}".format(index,product[0],product[1]))
products = [ ['iphone8',6888], ['MacPro', 14800], ['小米6',2499], ['coffee',31],['book',80],['Nike shoes',799]] shopping_cart = [] # 定義一個空的購物車 exit_flag = False # while True: while not exit_flag: print("商品信息".center(28, "-")) for index,product in enumerate(products): print("%s. %s %d" % (index,product[0],product[1])) product_choice = input("\n請輸入商品標號:\n") if product_choice.isdigit(): #判斷輸入的字符串是否只包含數字 product_choice = int(product_choice) # 字符串轉成整型 if product_choice >= 0 and product_choice < len(products): shopping_cart.append(products[product_choice][0]) #增長到購物車列表 print("\n商品 %s 已添加到購物車\n" % (products[product_choice][0])) else: print("商品標號有誤,請從新輸入") elif product_choice == "q": if len(shopping_cart) > 0: print("\n您添加到購物車的商品以下:\n") for index,product_cart in enumerate(shopping_cart): print("%s. %s" % (index,product_cart)) else: print("\n您的購物車爲空!\n") # break exit_flag = True #爲真時候結束循環