python数组列表、字典、拷贝、字符串

时间:2022-02-10 04:45:18

python中字符串方法

 name = "I teased at life as if it were a foolish game"
print(name.capitalize())#首字母大写
print(name.count("a"))#查找字符串中a的个数
print(name.center(50,"-"))#长度为50将name放中间不够的用-补全
print(name.endswith("ex"))#字符串是否以ex结尾 true \ false
print(name.expandtabs(tabsize=30))#将tab键转化为多少空格
print(name.find("k"))#字符串切片
print(name.format())#格式化输出
# print(name.format_map({'name'}))
print('lsjf342'.isalnum())#是否含有数字,有特殊符号不能检测
print('adK'.isalpha())#是否有大写字母
print(''.isdecimal())#是否为十进制数
print(''.isdigit())#是否为整数
print('a32f'.isidentifier())#是否为合法标识符
print(name.isspace())#是否为空格
print(name.istitle())#是否首字母大写
print(name.isprintable())#是否可以打印 字符串不考虑 tty file,drive file不可打印
print(name.isupper())#是否全为大写
print(''
'*'.join(['','','','']))#字符串拼接
print(name.ljust(80,"*"))#将字符串放在左边,长度不够80用*补全
print(name.rjust(10,""))
print(name.lower())#大写变小写
print(name.upper())#小写变大写
print('mark\n'.lstrip())#去左边的特殊符
print('\nmaek\n'.rstrip())
print('---')
p = str.maketrans("abcdef",'')#对应替换
print("mark".translate(p))
print(name.replace('a','A',2))#替换
print(name.rfind('A'))
print(name.split())#将字符串按照空格分成不同列表
print(name.split('r'))#按照r分成不同列表
print(name.splitlines())#按照换行分列表
print(name.swapcase())#大小写变换
print(name.title())#单词首字母大写
print(name.zfill(20))#不够20用0填充
结果输出:
1 D:\exploit\python\anaconda\python.exe D:/exploit/python/workSapce/day2/string.py
I teased at life as if it were a foolish game
5
--I teased at life as if it were a foolish game---
False
I teased at life as if it were a foolish game
-1
I teased at life as if it were a foolish game
True
True
True
True
True
False
False
True
False
1*2*3*4
I teased at life as if it were a foolish game***********************************
I teased at life as if it were a foolish game
i teased at life as if it were a foolish game
I TEASED AT LIFE AS IF IT WERE A FOOLISH GAME
mark maek
---
m1rk
I teAsed At life as if it were a foolish game
-1
['I', 'teased', 'at', 'life', 'as', 'if', 'it', 'were', 'a', 'foolish', 'game']
['I teased at life as if it we', 'e a foolish game']
['I teased at life as if it were a foolish game']
i TEASED AT LIFE AS IF IT WERE A FOOLISH GAME
I Teased At Life As If It Were A Foolish Game
I teased at life as if it were a foolish game Process finished with exit code 0

python中拷贝

 import copy#完全复制需要导入的包

 names = "ZhangYan Guyun DingXiaoPing"
names = ["ZhangYan","Guyuan","LiSi",["nothing","no"],"nothing","WangWu"] print(names[0:-1:2])
print(names[:])
print(names[::2])
for i in names:#切片
print(i) '''# name2 = names.copy() #浅拷贝
name2 = copy.deepcopy(names)#深层次拷贝
print(name2)
print(names)
names[2] = "橡皮"
names[3][1] = "神马"
print(names)
print(name2)#第一层拷贝的数据,第二层拷贝的是内存地址
''' '''names.append("nothing")#结尾插入
names.insert(2,"nomore")#任意位置插入
names[1] = "ZhangShan"#修改
print(names) # print(names.index("nothing"))#获得nothing的角标(第一次出现nothing的角标)
#
# print(names[names.index("nothing")])
#
# print(names.count("nothing"))#计数
#
# names.reverse()#反转
names.sort()#排序 ascii 码排序
print(names)
names2 = [1,2,3,4]
names.extend(names2)#合并
print(names,names2)
del names2#删除
print(names2)
# print(names[0],names[3])
# print(names[1:3])#取角标1和角标2
# print(names[2:])#取角标从2到结束
# print(names[-1])#取最后一位
# print(names[-2:])#取值后两位
# print(names[0:3])#零可以省略 # names.remove("nothing")
#
# del names[2]
# print(names)
#
names.pop()#括号内可以写角标
print(names)
'''
结果输出:
1 ['ZhangYan', 'LiSi', 'nothing']
['ZhangYan', 'Guyuan', 'LiSi', ['nothing', 'no'], 'nothing', 'WangWu']
['ZhangYan', 'LiSi', 'nothing']
ZhangYan
Guyuan
LiSi
['nothing', 'no']
nothing
WangWu

python字典

 #key-value
info = {
'stu1001': "NWuTengLan",
'stu1002': "LongZeLuoLa",
'stu1003': "MaLiYa", }
print(info)
print(info["stu1002"])#存在字典中才可用这种方式查找,容易出错
info["stu1002"]="泷泽萝拉"
info["stu1005"]="泷泽萝拉"#存在就修改,不存在就创建
print(info)
print(info.get("stu1004"))#不出错的查找 print('stu1002' in info)#判断是否在字典中 #del
# del info["stu1005"]删除
# info.pop("stu1005")删除
# info.popitem()随缘删除
print(info)
运行结果:
1 {'stu1003': 'MaLiYa', 'stu1002': 'LongZeLuoLa', 'stu1001': 'NWuTengLan'}
LongZeLuoLa
{'stu1003': 'MaLiYa', 'stu1005': '泷泽萝拉', 'stu1002': '泷泽萝拉', 'stu1001': 'NWuTengLan'}
None
True
{'stu1003': 'MaLiYa', 'stu1005': '泷泽萝拉', 'stu1002': '泷泽萝拉', 'stu1001': 'NWuTengLan'}

python数组列表

  输出数组第一层,让操作者输入数据,查看数据是否在第一层内,在的话就进入第二层,不在得话重新选择,以此类推。如果用户输入b返回,输入q退出

 data = {
'china':{
"北京":{
"朝阳区":["区*","七天酒店"],
"海淀区":["航空大学","地铁十号线"]
},
"河南":{
"郑州市":["桂林路","新区"],
"开封市":["民权","老城区"]
}, },
'USA':{
"加州":{},
"佛罗里达州":{}
}, } exitFlag = False while not exitFlag:
for i in data:
print(i)
choice = input("选择进入》》")
if choice in data:
for i2 in data[choice]:
print(i2)
choice2 = input("选择进入》》")
if choice2 in data[choice]:
for i3 in data[choice][choice2]:
print(i3)
choice3 = input("选择进入》》")
if choice3 in data[choice][choice2]:
for i4 in data[choice][choice2][choice3]:
print(i4)
choice4 = input("最后一层,按b返回》》")
if choice4 == "b" or choice4 == "B":
pass
elif choice4 =="q" or choice4 == "Q":
exitFlag = True#跳出循环
if choice3 == "b" or choice3 == "B":
pass#返回
elif choice3 == "q" or choice3 == "Q":
exitFlag = True
if choice2 == "b" or choice2 == "B":
break#跳出循环
elif choice2 =="q" or choice2 == "Q":
exitFlag = True