Python3.5--列表之深copy与浅copy、元组(下)

1、关于直接赋值


2、关于浅复制copy


(1)实现浅copy的三种方式:

import copy
person = ["name",["a",100]]
p1 = copy.copy(person)
p2 = person[:]
p3 = list(person)
print(p1)
print(p2)
print(p3)

#运行结果:
#['name', ['a', 100]] 
#['name', ['a', 100]] 
#['name', ['a', 100]]
(2)浅copy的应用:用来创建联合账号(如:夫妻共同存款)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import copy
person = ["name",["saving",100]]
p1 = copy.copy(person)
p2 = copy.copy(person)
p1[0] = "Liu"
p2[0] = "Mao"

p1[1][1] = 50   //当第一个人使联合账户的存款发生修改时,第二个人也能看到

print(p1)
print(p2)

#运行结果:
#['Liu', ['saving', 50]]
#['Mao', ['saving', 50]]

注:浅copy只复制列表的第一层元素。

3、关于深复制deep copy


4、元组(tuple)



     元组与列表差不多,也是存一组数,只是它一旦创建便不能再修改,所以,元组又叫只读列表。
     其语法为:采用小括号
names = ("Liu","Zhang","Mao","Song")
     元组只有2个方法,count和index。






3、列表结合各种逻辑条件应用——Python3.6-购物车程序练习实例

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

Products = [
    ('Iphone',5800),
    ('Mac',12000),
    ('Bike',800),
    ('Water',10)
]

shopping_list=[]

Salary = input("Your Salary:")
if Salary.isdigit():
    Salary = int(Salary)
    while True:
        for index,i in enumerate(Products):
            print(index,i)
        user_choice = input("Please input the products number:")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(Products) and user_choice >=0:
                if Products[user_choice][1] <= Salary:
                    shopping_list.append(Products[user_choice])
                    Salary -= Products[user_choice][1]
                    print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m"%(Products[user_choice],Salary))
                else:
                    print("\033[41;1mYour balance is not enough!\033[0m" %Salary)
            else:
                print("Invalid input!")
        elif user_choice == 'q':
            print("------------Shopping List----------")
            for i in shopping_list:
                print(i)
            print("Your current balance is:",Salary)
            exit()
        else:
            print("invalid option!")

'''
输出结果为:

Your Salary:34500
0 ('Iphone', 5800)
1 ('Mac', 12000)
2 ('Bike', 800)
3 ('Water', 10)
Please input the products number:1
Added ('Mac', 12000) into shopping cart,your current balance is 22500
0 ('Iphone', 5800)
1 ('Mac', 12000)
2 ('Bike', 800)
3 ('Water', 10)
Please input the products number:2
Added ('Bike', 800) into shopping cart,your current balance is 21700
0 ('Iphone', 5800)
1 ('Mac', 12000)
2 ('Bike', 800)
3 ('Water', 10)
Please input the products number:4
Invalid input!
0 ('Iphone', 5800)
1 ('Mac', 12000)
2 ('Bike', 800)
3 ('Water', 10)
Please input the products number:3
Added ('Water', 10) into shopping cart,your current balance is 21690
0 ('Iphone', 5800)
1 ('Mac', 12000)
2 ('Bike', 800)
3 ('Water', 10)
Please input the products number:q
------------Shopping List----------
('Mac', 12000)
('Bike', 800)
('Water', 10)
Your current balance is: 21690
'''




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值