列表&元组

names = [1,"r",2]
names.append("tr")  #追加
names.insert(0,"fjdk")
names.count("r")  #统计
names.index("r")  #返回某一个元素的下标

#删除某一个元素
names.remove(1)
del names[0]
pop(0)

names1 = [1.["think","java"],90]  #里面列表放的是地址
names1.copy()  #浅copy

name2 = names1  #地址赋值

import copy
copy.copy(names) #同copy()
copy.deepcopy(names) #深copy


#步长为2输出  切片
for i in range(0,10,2):
    print(i)
    
for i in names:
    print(i)
    
print(names[0:-1:2]) #0或-1可省略   print(names[::2])   print(names[:]) 输出全部  

浅copy方式

  • p = copy.copy(names) 同 names.copy()
  • p = names[ : ]
  • p = list(names )

元组

​ 元组和列表其实差不多,它也是一组数,只是它一旦创建,便不能修改,只能查看。

语法:

  • names = ("java","php",12)

方法:

  • count和index

返回下标的应用

goods = [
    ["Iphone",2300],  #或嵌套元组  ("Iphone",2300)  不可变更
    ["tree",12],
    ["flower",3]
]
for item in goods:
    print(goods.index(item), item[0],item[1])
    
'''
结果:
	1 Iphone 2300
    2 tree 12
    3 flower 3
'''

ps:
    # enumerate()可取下标
    
    for index,item in enumerate(goods):
        print(index+1,item)
        
'''
结果:
	1 ['Iphone', 2300]
    2 ['tree', 12]
    3 ['flower', 3]

'''
    
    

商品购买案例

  • 容错性差

    
    salary = int(input("please input your salary:\n"))
    select = [] #选中列表
    #商品列表
    goods = [["1.Iphone",2300],["2.dog",200],["3.bicycle",100],["4.shoe",34]]
    
    while True:
        print("please select your goods!")
        for i in goods:
            print(i)
        numStr = input("please input a number:")
        if numStr == "q":
            break;
        num = int(numStr)
        select.append(goods[num-1]) #追加商品到购物车
    
    for item in select:
        # print(item[1])
        salary -= item[1]
    
    if salary < 0:
        print("balance not enough! balance is:",salary)
    else:
        print("you buy below:")
        for i in select:
            print(i)
        print("your balance is:",salary,"thinks! bye")
    
    
    
    
  • 容错性强

    • 31 32 红 绿 字的颜色 41 42 红 绿 字的背景色

    • \033[31;1m ----- \033[0m

    
    goods_list = [
        ["Iphone",2300],
        ["dog",200],
        ["bicycle",100],
        ["shoe",34]
    ]
    
    select_list = []
    salary = input("please input your salary: ")
    if salary.isdigit(): #如果输入为数字形式则转化为整型
        salary = int(salary)
        while True:
            for index,item in enumerate(goods_list):
                print(index+1,item[0],item[1])
            num = input("please input a number: ")  #输入对应商品的编号
            if num.isdigit():   #如果输入为数字形式则转化为整型  输入非数字则打印错误信息“invalid option”
                num = int(num)
                if num <= len(goods_list) and num > 0:  #商品合理范围
                    goods_item = goods_list[num-1]    #取出商品列表中对应的项
                    if goods_item[1] <= salary: #可以购买
                        select_list.append(goods_item)
                        salary -= goods_item[1]     #金额进行减少
                        print("Added %s into shopping cart,current balance is \033[31;1m %s \033[0m" %(goods_item,salary))
                    else:
                        print("\033[41;1m 你的余额只剩 %s 了,不可以买了\033[0m"% salary)
                else:
                    #输入的商品编号不存在
                    print("product code \033[41;1m %s \033[0m is not exist"% num)
            elif num == "q":
                print("-------- shopping list --------")
                for index,item in enumerate(select_list):
                    print(index+1, item[0],item[1])
                print("your current balance: \033[31;1m %s \033[0m" % salary)
                exit()
            else:
                print("\033[41;1m invalid option \033[0m")
    
    
    
posted @ 2019-10-25 16:43  弘_毅  阅读(136)  评论(0)    收藏  举报