初识python:一些基础的知识(五)

不同数据类型的内置方法

在之前的博文中有提到关于字符串及列表的相关内置方法,在这一期中将详细就内置方法专门写一期博文

字符串的相关内置方法

字符串的长度

simple_str = 'aaabbbccc'
print(len(simple_str))

成员计算

simple_str = 'aaabbbccc'
print('aaa' in simple_str)
print('aaa' not in simple_str)

大小写转换

name = 'Wodetian'
name_up = name.upper()
name_low = name.lower()
print(name_up)
print(name_low)

格式化输出

name = 'Wodetian'

print(f'沃德天平常的表达是{name}') #以f-String格式化举例

查找和替换

hobby = 'wodetian love singing'

hobby.find('ing') #会返回从左往右第一个符合条件子串所在位置的索引值

hobby.rfind('ing') #这个函数恰恰相反,是从右往左找

print(hobby.replace('singing','playing Delta Force')) #前面放已经存在的子串,后面放要替代的子串

分割与连接

hobby = 'wodetian love singing' #输出值为一个列表,将字符串用指定的字符进行分割,并把分割后的各个元素组成一个列表
print(hobby.split(' '))

hobby_list = ['wodetian', 'love', 'singing'] #可以通过join函数使用指定的字符串来连接列表中的各个元素并输出为一个新的字符串
' '.join(hobby_list)

去除字符串首尾空白

nonsense_word = ' wuhu,qifei. ' #可以去除字符串的首尾空白字符并将结果输出为一个新的字符串(默认)
print(nonsense_word.strip())
print(nonsense_word.replace(' ','#').lstrip('#')) #同时这个函数可以去除指定方向的字符(从左去除举例)
print(nonsense_word.replace(' ','#').rstrip('#ei.f')) #strip函数中的字符串无顺序关系,只要包含就去除

有趣的显示函数

text = '实例文本'
print(text.center(50,'-')) #生成一个长度50的字符串,两边用‘-’进行填充

print(text.ljust(50,''))
print(text.rjust(50,'
'))

文本转换相关的函数

text = 'hElLo worLD'

print(text.capitalize()) #使第一个字符大写,其他字符小写

print(text.swapcase()) #使大小写翻转

print(text.title()) #使每一个开头的字符大写

判断是否用某个字符串开头

text = 'hello world'
print(text.startswith('hel')) #判断是否以指定字符串开始,同时这个函数后面还能传进去两个参数(start,end)用于判断指定子串
print(text.startswith('leh')) #注意有顺序
print(text.endswith('ld')) #判断是否以指定字符串结束
print(text.endswith('dl')) #同理

列表的相关内置方法

列表的长度

gun_list = ['qbz191','qbz95','qcq171','xm7','hk416']

print(len(gun_list))

列表成员运算

print('qbz95' in gun_list)
print('xm250' not in gun_list)

追加列表成员

gun_list.append('xm250') #append函数会将新成员加在列表尾部
print(gun_list)

删除列表成员

del gun_list[-1]
print(gun_list)

gun_list.pop(1) #如果不指定索引默认删除最后一个元素
print(gun_list)

gun_list.remove('xm7')
print(gun_list)

循环列表中的元素

for i in gun_list:
print(i)

向列表中插入新的元素

gun_list.insert(4,'xm250') #在指定的索引值前插入元素
print(gun_list)

统计列表中指定元素的个数

print(gun_list.count('hk416'))

获取列表指定元素的索引

print(gun_list.index('xm250'))

清空列表

gun_list.clear()
print(gun_list)

复制列表

gun_list = ['qbz191','qbz95','qcq171','xm7','hk416']
gun_list1 = gun_list.copy()
print(gun_list1)

扩展列表

new_gun_list = ['m4','ash','as-val']
gun_list.extend(new_gun_list) #将指定列表扩展到原列表后方
print(gun_list)

翻转列表

gun_list.reverse()
print(gun_list)

对列表进行排序

gun_list.sort()
print(gun_list)

元组的相关内置方法(了解即可)

在之前的博文中没有提及到元组这一概念,大家可以理解为一个类似于列表一样的数据类型,但是它和列表的区别就在于元组中的元素只能取不能修改,一但定义完成便无法修改。

定义的方法为 tuple() 或 直接用()扩起来,其中元素可以为任意数据类型,举例:

new_tuple1 = ('thing1','thing2')
new_tuple2 = tuple(('thing3','thing4'))
new_tuple3 = tuple('wuhu')
print(new_tuple1)
print(new_tuple2)
print(new_tuple3)

元组的索引取值

print(new_tuple1[0])

print(new_tuple1[-2:])

元组的长度

print(len(new_tuple1))

元组的成员运算

print('thing1' in new_tuple1)

元组的循环遍历

for i in new_tuple1:
print(i)

元组中元素的出现次数

print(new_tuple1.count('thing1'))

元组中指定元素的索引

print(new_tuple1.index('thing1'))

字典的相关内置方法

字典的定义:使用{},内部使用键值对(key:value)进行存储。一般key为字符串,用于描述与之相对应的value的特征,value可为任何数据类型(key其实也可为任何数据类型)。

wodetian_info_dict = {
'name' : 'wodetian',
'sex' : 'female',
'height' : 180 ,
'weight' : 130
}

wodetian_info_dict['name'] ##这里展示两种从字典中取值的方法

wodetian_info_dict.get('name') #get的优点:如果键不存在会返回一个None,并不会报错

wodetian_info_dict.get('gun',666) #同时可以通过设定参数达到找不到键返回指定值的效果

字典的长度

print(len(wodetian_info_dict))

字典的成员运算

print('name' in wodetian_info_dict)

print('wodetian' in wodetian_info_dict) #成员运算只对key有效

字典的删除操作

wodetian_info_dict.pop('sex') #删除指定key对应的键值对

print(wodetian_info_dict)

字典遍历

把以下方法取出的所有值当做列表来处理

wodetian_info_dict.keys()

wodetian_info_dict.values()

wodetian_info_dict.items()

同时有一个操作,就是可以使用列表解压缩的方式来直接获取键值对的值,举例:

for k,v in wodetian_info_dict.items():
print(k,v)

字典的更新

new_dict = {
'wuhu' : 'yiwu',
'kingoflitang' : 'dingzhen',
'weight' : 140
}

wodetian_info_dict.update(new_dict) #参数是一个字典,新字典中的键值对有则更新,无则添加

for k,v in wodetian_info_dict.items():
print(f"{k}:{v}")

字典的formkeys方法(有点抽象了解即可)

dic = dict.fromkeys(['a','b'],'d') #将第一个数组参数中的值依次取出来当key,对应的value为第二个参数

print(dic)

字典的有条件添加元素

wodetian_info_dict

wodetian_info_dict.setdefault('wuhu','qifei') #第一个参数是key,第二个参数是value

wodetian_info_dict.setdefault('relx','5') #如果参数中的key已经存在对应的键值对则不变,如果没有则添加

wodetian_info_dict

集合的相关内置方法(了解即可)

集合(set),对,就是大家想的那个数学里面的集合,python里面有一个专门的数据类型用于描述它。

首先是集合的定义方式,集合是用{}表示的,中间的元素使用逗号进行分割,实例:

new_set1 = {1,4,5,2,1,'a','g'} #注意集合中只能存放不可变的数据类型,同时重复的元素会自动去重
print(new_set1) #去重的同时还会打乱元素原有的顺序

new_set2 = set() #定义一个空的集合
print(new_set2)

集合的长度

print(len(new_set1))

集合的成员运算

print('a' in new_set1)

集合之间的相关运算

并集

new_set2 = {2,3,5,6,'v','c','g'}

print(new_set1 | new_set2)

print(new_set1.union(new_set2))

交集

print(new_set1.intersection(new_set2))

差集

print(new_set1 - new_set2)

print(new_set1.difference(new_set2))

对称差集

print(new_set1 ^ new_set2) #并集-交集的结果

print(new_set1.symmetric_difference(new_set2))

子集判断

测试一个集合是否为另外一个集合的子集

print(new_set1 > new_set2) #判断new_set2是否为new_set1的子集

father_set = {1,2,3,4,5}
son_set = {1,4}
print(father_set > son_set) #判断son_set是否为father_set的子集

给集合添加元素

print(father_set)
father_set.add('a')
print(father_set)

删除集合中的指定元素

print(father_set)
father_set.remove('a')
print(father_set)
father_set.discard("sing,dance,rap,basketball") #discard相较于remove的区别是discard不会报错

从一个集合中删除另一个集合的所有元素

print(father_set)
print(son_set)
father_set.difference_update(son_set)
print(father_set)

判断两个集合交集是否为空

print(father_set.isdisjoint(son_set))

posted @ 2025-09-07 21:16  沃德天sama  阅读(10)  评论(0)    收藏  举报
1