列表浅谈,字符串补充

来源:互联网 发布:js音乐播放器插件 编辑:程序博客网 时间:2024/05/21 11:33

p=str.maketrans('abcdefg','1234567')print('love'.translate(p))类似于编码加密
print('I Love you'.swapcase())小写变大写,大写变小写

补充字符串

python中列表浅复制

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

夫妻列表:

我,凤姐  ,银行联合账号,就是账户名不同,银行里的钱一样,不严谨的表达,哈哈哈

person=['name',['saving',100]]p1=person[:]p2=person[:]p1[0]='我'p2[0]='凤姐'p1[1][1]=50  #我用了50元,所以凤姐也少了50元print(p1)print(p2)
补充下元组tuple的方法,只有两种,count,index

----------------购物车程序--------------------

#!/usr/bin/env python# -*- coding:utf-8 -*-# Author:cokeshopping_list=[]product_list=[    ['iphone',5800],    ('mac pro',9800),    ('bike',800),    ('watch',10600),    ('coffee',31),    ('alex python',120),]salary = input("input your salary")if salary.isdigit():    salary=int(salary)    while True:        for index,item in enumerate(product_list):          #enumerate使得返回            print(index,item)        user_choice=input("what are your needs")        if user_choice.isdigit():            user_choice=int(user_choice)            if user_choice<len(product_list) and user_choice >-1:                p_item=product_list[user_choice]                if p_item[1]<salary:                    shopping_list.append(p_item)                    salary-=p_item[1]                                #高亮输出  红色33                    print("Added %s into shopping cart,your shopping balance is \033[31;1m%s\033[0m"%(p_item,salary))                else:                    print('\033[41;1mfoolish,your money is [%s]——poorman,go die!\033[0m'%salary)            else:                print('your choice[%s] is not found'%user_choice)        elif user_choice=='q':            print('-----shoppinglist-----')            for p in shopping_list:                print(p)            print('your current balance :',salary)            exit()        else:            print('invalid option')