欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-12 16:33:18
python中字符串方法 python中拷贝 python字典 python数组列表 输出数组第一层,让操作者输入数据,查看数据是否在第一层内,在的话就进入第二层,不在得话重新选择,以此类推。如果用户输入b返回,输入q退出 ......

python中字符串方法

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

 

python中拷贝

 1 import copy#完全复制需要导入的包
 2 
 3 names = "ZhangYan Guyun DingXiaoPing"
 4 names = ["ZhangYan","Guyuan","LiSi",["nothing","no"],"nothing","WangWu"]
 5 
 6 print(names[0:-1:2])
 7 print(names[:])
 8 print(names[::2])
 9 for i in names:#切片
10     print(i)
11 
12 '''# name2 = names.copy() #浅拷贝
13 name2 = copy.deepcopy(names)#深层次拷贝
14 print(name2)
15 print(names)
16 names[2] = "橡皮"
17 names[3][1] = "神马"
18 print(names)
19 print(name2)#第一层拷贝的数据,第二层拷贝的是内存地址
20 '''
21 
22 
23 '''names.append("nothing")#结尾插入
24 names.insert(2,"nomore")#任意位置插入
25 names[1] = "ZhangShan"#修改
26 print(names)
27 
28 # print(names.index("nothing"))#获得nothing的角标(第一次出现nothing的角标)
29 #
30 # print(names[names.index("nothing")])
31 #
32 # print(names.count("nothing"))#计数
33 #
34 # names.reverse()#反转
35 names.sort()#排序 ascii 码排序
36 print(names)
37 names2 = [1,2,3,4]
38 names.extend(names2)#合并
39 print(names,names2)
40 del names2#删除
41 print(names2)
42 # print(names[0],names[3])
43 # print(names[1:3])#取角标1和角标2
44 # print(names[2:])#取角标从2到结束
45 # print(names[-1])#取最后一位
46 # print(names[-2:])#取值后两位
47 # print(names[0:3])#零可以省略
48 
49 # names.remove("nothing")
50 #
51 # del names[2]
52 # print(names)
53 #
54 names.pop()#括号内可以写角标
55 print(names)
56 '''
结果输出:
1 ['ZhangYan', 'LiSi', 'nothing'] 2 ['ZhangYan', 'Guyuan', 'LiSi', ['nothing', 'no'], 'nothing', 'WangWu'] 3 ['ZhangYan', 'LiSi', 'nothing'] 4 ZhangYan 5 Guyuan 6 LiSi 7 ['nothing', 'no'] 8 nothing 9 WangWu

python字典

 1 #key-value
 2 info = {
 3     'stu1001': "NWuTengLan",
 4     'stu1002': "LongZeLuoLa",
 5     'stu1003': "MaLiYa",
 6 
 7 }
 8 print(info)
 9 print(info["stu1002"])#存在字典中才可用这种方式查找,容易出错
10 info["stu1002"]="泷泽萝拉"
11 info["stu1005"]="泷泽萝拉"#存在就修改,不存在就创建
12 print(info)
13 print(info.get("stu1004"))#不出错的查找
14 
15 print('stu1002' in info)#判断是否在字典中
16 
17 #del
18 # del info["stu1005"]删除
19 # info.pop("stu1005")删除
20 # info.popitem()随缘删除
21 print(info)
运行结果:
1 {'stu1003': 'MaLiYa', 'stu1002': 'LongZeLuoLa', 'stu1001': 'NWuTengLan'} 2 LongZeLuoLa 3 {'stu1003': 'MaLiYa', 'stu1005': '泷泽萝拉', 'stu1002': '泷泽萝拉', 'stu1001': 'NWuTengLan'} 4 None 5 True 6 {'stu1003': 'MaLiYa', 'stu1005': '泷泽萝拉', 'stu1002': '泷泽萝拉', 'stu1001': 'NWuTengLan'}

python数组列表

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

 1 data = {
 2     'china':{
 3         "北京":{
 4             "朝阳区":["区*","七天酒店"],
 5             "海淀区":["航空大学","地铁十号线"]
 6         },
 7         "河南":{
 8             "郑州市":["桂林路","新区"],
 9             "开封市":["民权","老城区"]
10         },
11 
12     },
13     'USA':{
14         "加州":{},
15         "佛罗里达州":{}
16     },
17 
18 }
19 
20 exitFlag = False
21 
22 while not exitFlag:
23     for i in data:
24         print(i)
25     choice = input("选择进入》》")
26     if choice in data:
27         for i2 in data[choice]:
28             print(i2)
29         choice2 = input("选择进入》》")
30         if choice2 in data[choice]:
31             for i3 in data[choice][choice2]:
32                 print(i3)
33             choice3 = input("选择进入》》")
34             if choice3 in data[choice][choice2]:
35                 for i4 in data[choice][choice2][choice3]:
36                     print(i4)
37                 choice4 = input("最后一层,按b返回》》")
38                 if choice4 == "b" or choice4 == "B":
39                     pass
40                 elif choice4 =="q" or choice4 == "Q":
41                     exitFlag = True#跳出循环
42             if choice3 == "b" or choice3 == "B":
43                 pass#返回
44             elif choice3 == "q" or choice3 == "Q":
45                 exitFlag = True
46         if choice2 == "b" or choice2 == "B":
47             break#跳出循环
48         elif choice2 =="q" or choice2 == "Q":
49             exitFlag = True