自学Python-2 week!
一、copy用法
names2=names.copy()##浅copy假copy,无法改列表中带列表,因为存储模式关系,列表中带列表存储为单独的片段。
浅copy的实现形式
p1=copy.copy(names)
p2=names[:]
p3=list(names)
浅copy的用处如联合账号!
import copy
names2=names.deepcopy(names)##深copy 深copy需要引用模块。
二、字符串操作
name=”Laugh snail”
names=”my name is {name} and i am {year} old”
print(name.capitalize())#首字母大写
print(name.count(“a”))#查找a的个数
print(name.center(50,”-“))#打印50个字符,不够用-补全
print(name.endswith(“il”))#判断是否以il结尾
print(name.find(“s”))#查找的底标
print(names.format(name=”snail”,year=”23″))#给{name}赋值
print(names.format_map({“name”:”snail”,”year”:”23″}))#字典形式
print(“abc123”.isalnum())#是否是数字和字母
print(“abc123”.isalpha())#是否是纯英文字符
print(“abc123”.isdecimal())#是否是十进制
print(“abc123”.isdigit())#是否是整数
print(“abc123”.isidentifier())#判断是不是合法的标识符
print(“abc123”.isspace())#判断是否是一个空格
print(“Abc123”.istitle())#判断首字母是否大写
print(“Abc123”.isprintable())#判断是否可以打印的文件
print(“Abc123”.isupper())#是否全是大写
print(“+”.join([“1″,”2″,”3″,”4”]))#把字符串用+相连
print(name.ljust(50,”*”))#字符串50个字符,不够用*在右侧补全
print(name.rjust(50,”*”))#左
print(“Aame”.lower())#改全小写
print(“Aame”.upper())#改全大写
print(”
snail
“.lstrip())#
print(”
snail
“.rstrip())#
print(”
snail
“.strip())
p=str.maketrans(“abcdef”,”123456″)
print(“snail”.translate(p))
print(“snaill”.replace(“l”,”L”,1))
print(“snaill”.rfind(“l”))#找到最右边值的下标
print(“s n ai l”.split(”
“))#根据空格取值
print(“s n ai l”.split(” “))
print(“s n
ai l”.splitlines())
print(“Small Snail”.swapcase())#大小写互换
print(“mall Snail”.title())#首字母大写
print(“small snail”.zfill(20))#用0补全20位
三、小练习-简单菜单制作
lunch_list=[
("米饭",3),
("可乐",5),
("虎皮辣子烧茄子",20),
("西辣蛋",18),
("小炒肉",25),
("麻辣虾尾",45),
("酸辣土豆丝",10),
("蒜苔炒肉",20),
("馍馍",2),
]
shopping_list=[]
money_spend=input("你钱包有多少钱:")
if money_spend.isdigit():##如果输入的是整数
money_spend=int(money_spend)##数值整数型
while True:
for index,item in enumerate(lunch_list):
#print(lunch_list.index(item),item)
print(index,item)
user_choice=input("请填写点餐序号,或输入q退出>>>:")
if user_choice.isdigit():
user_choice=int(user_choice)
if user_choice<len(lunch_list) and user_choice>=0:
P_item=lunch_list[user_choice]
if P_item[1]<=money_spend:#买的起
shopping_list.append(P_item)
money_spend-=P_item[1]
print("你选择的商品是 %s ,你的余额是 33[31;1m%s33[0m" % (P_item, money_spend))
else:
print("33[41;1m你的余额只剩[%s]啦,还买个毛线33[0m" % money_spend)
elif user_choice>len(lunch_list):
print("不好意思无此商品!请睁大眼睛瞧仔细,重新选择。")
else:
print("您所点的商品如下[%s] " % user_choice)
elif user_choice == "q":
print("--------Lunch list------")
for p in shopping_list:
print(p)
print("你的余额是:", money_spend)
exit()
else:
print("输入错误!请输入序号。")
四、字典操作
info = {
"stu1101": "TengLan Wu",
"stu1102": "LongZe Luola",
"stu1103": "XiaoZe Maliya",
}
print(info)
print(info["stu1101"])#输出key所对应的值
info["stu1101"]="武藤兰"#修改
info["stu1104"]="cangjingkong"#增加
#del info["stu1101"]#删除
info.pop("stu1101")
info.popitem()#随机删除
print(info)
1,字典的合并
info = {
"stu1101": "TengLan Wu",
"stu1102": "LongZe Luola",
"stu1103": "XiaoZe Maliya",
}
b={
"stu1101":"renzao",
1:2,
3:4
}
print(info.get(""))#安全查找
print("stu1103" in info)#查找是否在字典里
print(info.values())#取字典中所有的值
print(info.keys())#字典所有的key
info.update(b)#合并更新info字典
print(info)
c=dict.fromkeys([6,7,8],"test")#新建字典并赋值
print(c)
print(info.items())#打印字典到列表
2.多层字典操作
av_catalog = {
"欧美":{
"www.youporn.com": ["很多免费的,世界最大的","质量一般"],
"www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
"letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
"x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]
},
"日韩":{
"tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
},
"大陆":{
"1024":["全部免费,真好,好人一生平安","服务器在国外,慢"]
}
}
av_catalog["大陆"]["1024"][1]="可以在国内做镜像"
av_catalog.setdefault("taiwan",{"www.baidu.com":[1,2]})#字典里没有则增加,有不做更改。
print(av_catalog)
3.字典的循环
info = {
"stu1101": "TengLan Wu",
"stu1102": "LongZe Luola",
"stu1103": "XiaoZe Maliya",
}
for i in info: #字典的循环
print(i,info[i])
4.三级字典
# Author:linhui liu
data = {
"北京":{
"昌平":{
"沙河":["oldboy","test"],
"天通苑":["链家地产","我爱我家"]
},
"朝阳":{
"望京":["奔驰","陌陌"],
"国贸":{"CICC","HP"},
"东直门":{"Advent","飞信"},
},
"海淀":{},
},
"山东":{
"德州":{},
"青岛":{},
"济南":{}
},
"广东":{
"东莞":{},
"常熟":{},
"佛山":{},
},
}
exit_flag = False
while not exit_flag:
for i in data:
print(i)
choice = input("选择进入1>>:")
if choice in data:
while not exit_flag:
for i2 in data[choice]:
print(" ",i2)
choice2 = input("选择进入2>>:")
if choice2 in data[choice]:
while not exit_flag:
for i3 in data[choice][choice2]:
print(" ", i3)
choice3 = input("选择进入3>>:")
if choice3 in data[choice][choice2]:
for i4 in data[choice][choice2][choice3]:
print(" ",i4)
choice4 = input("最后一层,按b返回>>:")
if choice4 == "b":
pass
elif choice4 == "q":
exit_flag = True
if choice3 == "b":
break
elif choice3 == "q":
exit_flag = True
if choice2 == "b":
break
elif choice2 == "q":
exit_flag = True
加油!坚持!