码迷,mamicode.com
首页 > 其他好文 > 详细

字符串、列表、字典和集合

时间:2018-08-27 12:49:18      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:查找   case   列表   统计   copy   strip()   次数   cap   from   

一、字符串常用方法

 1 #!/usr/bin/env python
 2 #created by Baird
 3 
 4 str = "my name is Baird."
 5 str2= "my name is {name},I‘m from {nation}"
 6 
 7 print(str.capitalize())     #转换成大写
 8 print(str.count("a"))       #统计字符a出现次数
 9 print(str.center(50,"*"))   #字符串居中显示,长度50,不足时用*填充
10 print(str.endswith("rd."))  #判断是否以 rd. 结束
11 print(str.expandtabs(tabsize=30))   #将\t转换成30个空格
12 print(str.find("name"))     #返回 name 第一次出现的位置,失败返回-1
13 print(str2.format(name=Baird,nation=China)) #格式化输出
14 print(str2.format_map({name:Baird,nation:China}))   #以字典格式化输出
15 print("abc123".isalnum())       #是否为纯拉丁字符和阿拉伯数字
16 print("abc".isalpha())          #是否为纯拉丁字符
17 print("123".isdigit())          #是否为整数
18 print("123x".isdecimal())       #是否为十进制数
19 print(str.islower())            #是否为小写
20 print("ABC".isupper())          #是否为大写
21 
22 print("+".join([1,2,3]))  #将 + 穿插在123之中,输出为 1+2+3
23 
24 print(str.ljust(50,"*"))        #字符串居左,长度不足50时用*填充
25 print(str.rjust(50,"*"))        #字符串居右,长度不足50时用*填充
26 
27 print("ABC".lower())    #转换成小写
28 print("abc".upper())    #转换成大写
29 
30 print("\n123\n".strip())    #去除字符串中的换行
31 print("\n456\n".lstrip())   #去除字符串左边的换行
32 print("\n789\n".rstrip())   #去除字符串右边的换行
33 
34 encryption = str.maketrans("ni","52")   #以n->5,i->2的形式,返回编码的字典
35 print(encryption)
36 print(str.translate(encryption))        #以encryption(字典类型)进行编码
37 
38 decryption = str.maketrans("52","ni")   #解码
39 print(str.translate(encryption).translate(decryption))  #解码
40 
41 print("abac".replace("a","A",1))    #将字符串中第一个 a 转换成 A
42 
43 print(str.rfind("m"))   #返回最后一个 m 的位置
44 print(str.find("m"))    #返回第一个 m 的位置
45 
46 print(str.split())      #分割字符串为列表
47 print(str.split("m"))    #m为分割点
48 
49 print("abcABC".swapcase())  #大小写互换
50 
51 print("english speech and pronunciation".title())   #英文单词首字母变大写

 

二、列表常用方法

 1 #!/usr/bin/env python
 2 #created by Baird
 3 
 4 names = ["托塔天王","玉皇大帝","孙悟空","唐僧","白龙马","孙悟空"]
 5 
 6 print(names[0:2])   #切片,左闭合右开
 7 print(names[0:-1])  #-1表示从右数第一个
 8 print(names[-2:])   #从倒数第二个到最后
 9 
10 names.append("猪八戒") #添加元素
11 print(names)
12 
13 names.insert(1,"杨戬")    #插入元素,位置1处开始所有元素后移
14 print(names)
15 
16 names[1] = "哪吒"     #修改下标为1的元素
17 print(names)
18 
19 #names.remove("哪吒")    #按值删除
20 #del names[1]           #按位置删除
21 names.pop(1)            #按位置删除
22 print(names)
23 
24 print(names.count("孙悟空"))             #统计
25 print(names[names.index("孙悟空")])      #按值查找,index取下标
26 
27 names.reverse()                #反转
28 print(names)
29 
30 names.sort()                   #排序
31 print(names)
32 
33 names2 = [1,2,3,4]
34 names.extend(names2)           #扩展
35 print(names)
36 
37 del names2               #删除names2后不影响names
38 print("line38",names)
39 
40 
41 names4 = ["托塔天王",["玉皇大帝","王母娘娘"],"孙悟空","唐僧","白龙马","孙悟空"]
42 names5=names4.copy()    #列表中的列表复制方式为引用,names5[1]和names4[1]的值始终一致
43 names4[0]="沙悟净"
44 names4[1][1]="如来佛祖"
45 print("names5",names5)
46 print("names4",names4)

 

三、字典常用方法

 1 #!/usr/bin/env python
 2 #created by Baird
 3 
 4 #字典是无序的
 5 info = {
 6     1001:张无忌,
 7     1002:张三丰,
 8     1003:张翠山,
 9     1004:杨逍
10 }
11 
12 print(info.get(1006))     #按key查找,失败返回None
13 print(1003 in info)       #按key 查找,返回bool
14 
15 info[1005] = 白眉鹰王   #添加元素
16 print(info)
17 print(info.values())    #只打印值
18 
19 del info[1005]        #按key删除
20 print(info)
21 
22 info.setdefault(1001,赵敏)
23 print(info)
24 
25 info.setdefault(1005,赵敏)    #添加元素,同add
26 print(info)
27 
28 info2={
29     1001:孙悟空,
30     1007:唐僧
31 }
32 info.update(info2)      #以字典info2为base,更新字典info,key存在则修改值,不存在则增加键值对
33 print(info)
34 
35 print(info.items())     #字典转换成列表
36 
37 c = dict.fromkeys([1,2,3],temp)      #创建具有临时值的字典,1、2、3为key,值均为temp
38 c[1] = ball
39 print(c)
40 
41 c = dict.fromkeys([1,2,3],{name:Jack})   #创建具有临时值的字典,三个key的值均为同一引用
42 c[1][name] = Ross
43 print(c)

 

四、集合基本操作

 1 #!/usr/bin/env python
 2 #created by Baird
 3 
 4 list_1 = [1,2,3,4,5,6,7]
 5 
 6 list_1 = set(list_1)    #创建集合
 7 
 8 print(list_1)            #集合是无序的
 9 print(type(list_1))
10 
11 list_2 = set([1,2,3])
12 
13 print(list_1.intersection(list_2))  #交集 相当于 print(list_1 & list_2)
14 print(list_1.union(list_2))         #并集 相当于 print(list_1 | list_2)
15 print(list_1.difference(list_2))    #相当于 print(list_1 - list_2)
16 print(list_1.issubset(list_2))     #子集判断吗,list1是否属于list2
17 print(list_1.issuperset(list_2))   #父集判断
18 print(list_1.symmetric_difference(list_2))
19 # (list_1 & list_2) - (list_1 | list_2) 或 list_1 ^ list_2
20 
21 print(list_1.isdisjoint(list_2))   #若无交集,则为True
22 
23 list_1.add(10)    #添加一个元素
24 print(list_1)
25 list_1.update([11,12,13])   #添加多个元素
26 print(list_1)
27 list_1.remove(13)      #删除一个元素
28 print(list_1)
29 list_1.discard(12)      #元素存在则删除,不存在则忽视
30 print(list_1)
31 
32 if 11 in list_1:     #元素存在判断
33     print("Yes")
34 else:
35     print("No")

 

字符串、列表、字典和集合

标签:查找   case   列表   统计   copy   strip()   次数   cap   from   

原文地址:https://www.cnblogs.com/baird/p/9541340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!