Python封装函数之str、list、tuple、dict、set

  • 字符
encode
decode
test = 'aLexaLex'
v = test.find('ex',5,7)    #默认从开始往后找现在设置5到7的闭开区间
print(v)                   #找到第一个之后,获取其位置,没找到返回-1


test = '你是风儿我是沙'
print(test)
t = ' '
v = t.join(test)               #将字符串中的每一个元素按照指定分隔符进行拼接
print(v)

test = 'aLex'
v1 = test.islower()              #判断全是小写
v3 = test.isupper()              #判断全是大写
v2 = test.lower()                #全是变成小写
v4= test.upper()                 #全是变成大写
print(v1,v2,v3,v4)

test = 'testasdsdfgs'
v1 = test.rpartition('s')               #找到最右边的字符分割成3份
v2 = test.partition('s')                #找到最左边的字符分割成3份
v3 = test.rsplit('s',2)                 #找到字符(默认全部,这找2个从右边开始),去除字符并分割
v4 = test.split('s')                    #找到字符,去除字符并分割
print(v1,v2,v3,v4)

test = '\n1 \naLe xx'
v1 = test.lstrip()               #去除空白\n\t,从最左边开始连续的
v2 = test.rstrip()                #去除空白\n\t,从最右边开始连续的
v3 = test.strip()                #去除空白\n\t,从2边开始连续的
v4 = test.rstrip('x')            #指定去除x,从2变开始连续的
print(v1,v2,v3,v4)

test = 'aLex'
v = test[0:2]
v = len(test)  #可以读列表元素
index = 0
while index < len(test):
    index += 1
for index in test:
    print(index)
v = range(0,100,5)   #创建一个100个数的数组,for循环才真实创建,默认0开始,可设定0到100步长5
for item in rang(0,len(test)):
    print(item,test[item])
print('=====================')
test = 'aLex'
v = test.capitalize()   #首字母大写其他字母小写
print(v)

test = 'aLex'
v1 = test.casefold()      #将字符串中所有语言中大写变小写
print(v1)
v2 = test.lower()         #将字符串中英文大写变小写
print(v2)

test = 'aLex'
v = test.center(20,'*')    #设置宽度,并内容居中,20代指总长度,*空白未知填充,一个字符,可有可无
v1 = test.ljust(20,'*')    #设置宽度,并内容居左,20代指总长度,*空白未知填充,一个字符,可有可无
v2 = test.rjust(20,'*')    #设置宽度,并内容居右,20代指总长度,*空白未知填充,一个字符,可有可无
v3 = test.zfill(20)        #设置宽度,并内容居右,20代指总长度,只用0来填充
print(v,v1,v2,v3)

test = 'aLexaLex'
v = name.count('ex')       #去字符串中寻找子序列出现次数
print(v)

test = 'aLexaLex'
v = name.count('ex',6,8)   #数字符出现次数,默认全部,设定从第6个到第8个的闭开区间
print(v)

test = 'aLex'
v = test.endswith('ex')      #判断以什么结尾
print(v)
v = test.startwith('ex')     #判断以什么开始
print(v)

test = "username\temail\tpassword\nlaiying\tying@qq.com\t123\nlaiying\tying@qq.com\t123\nlaiying\tying@qq.com\t123"
v = test.expandtabs(20)         #\t是制表符,expandtabs,断句20,到\t不足20补空格
print(v,len(v))



test = 'i am {name},age {a}'
print(test)
v = test.format(name='alex',a=19)  #格式化,将一个字符串中的占位符替换为指定的值
print(v)
test = 'i am {0},age {1}'
print(test)
v = test.format('alex',19)         #格式化,将一个字符串中的占位符替换为指定位置的值
print(v)

test = 'i am {name},age {a}'
v = test.format_map({"name":'alex',"a":19})  #格式化,传入的值只能是字典
print(v)

test = 'aLexaLex'
v = test.index('ex',5,8)    #默认从开始往后找现在设置5到7的闭开区间
print(v)                   #找到第一个之后,获取其位置,没找到报错

test = "uasf890"
v = test.isalnum()          #判断字符串中字符串中是否只包含数字和字母
print(v)

test = "uasf"
v = test.isalpha()           #判断字符串中字符串中是否只包含字母
print(v)

test = "二"
v1 = test.isdecimal()          #判断字符串中字符串中是否只包含数字 2
v2 = test.isdigit()            #判断字符串中字符串中是否只包含数字(还有数字符号)②
v3 = test.isnumeric()          #判断字符串中字符串中是否只包含数字(还有数字符号、汉字)二
print(v1,v2,v3)

a = "la_123"
v = a.isidentifier()           #判断标志符 标识符只包含,字母、数字、下划线且首字母不是数字
print(v)

test = 'oiue\turyyffb'
v = test.isprintable()           #判断字符串是否存在不可显示的字符\t制表\n换行
print(v)


test = ' '
v = test.isspace()                 #判断字符全空格
print(v)

test = 'sdusdjnd and sdin is hwj'
v1 = test.istitle()                 #判断标题,也是所有单词首字母大写
test1 = test.title()
v2 = test1.istitle()
print(v1,test1,v2)

v = 'aesdioujkdfhiudoiasdoiuf'
m = str.maketrans('aeiou','12345')  #设定前后对应关系
new_v = v.translate(m)              #对m中字符进行对应关系变换
print(new_v)

v = 'alexalex'
new_v = v.replace('ex','apeiou',2)     #对匹配到的序列替换,默认全替换,现在只替换前2个
print(new_v)

test = 'asdfadsf\nasdasfdsf\nsdfa'
v = test.splitlines(True)                #分割,只能根据,true,false;是否保留换行
print(v)

test = 'backend1.1.1.1'
v  = test.startswith('a')                #判断以XXX开头
v1 = test.endswith('a')                  #判断以XXX结尾
print(v,v1)

test = 'aLex1'
v = test.swapcase()                      #大小写转换
print(v)
  • 列表
li = [11, 22, 33, 44]
v = li.append(5)     #直接修改list无返回值,元素做整体追加
print(li)
li = [11, 22, 33, 44]
li.extend([22])           #把元素循环扩展到列表中
print(li)

li = [11, 22, 33, 44]
li.clear()           #清空
print(li)

li = [11, 22, 33, 44]
v = li.copy()          #浅拷贝
print(v)

li = [11, 22, 33, 44]
v = li.count(22)         #计算元素出现的个数
print(v)

li = [11, 22, 33, 44]
v = li.index(22)           #根据值获取当前值获取位置
print(v)

li = [11, 22, 33, 22, 44]
li.insert(0, 99)           #在指定索引位置插入元素
print(li)

li = [11, 22, 33, 22, 44]
v = li.pop()               #默认删除最后一个位置的值并返回当前值
print(li,v)

li = [11, 22, 33, 22, 44]
li.remove(11)               #删除第一个指定值
print(li)

li = [11, 22, 33, 22, 44]
li.remove(11)               #删除第一个指定值
print(li)

li = [11, 22, 33, 22, 44]
li.reverse()               #将当前列表反转
print(li)

li = [11, 22, 33, 22, 44]
li.sort(reverse=True)               #列表从小到大排序,现在从大到小
print(li)


li = [1, 2, 43, [1, 2, 3], "age", "alex"]
for item in range(0, 5):
    print(li[item])
print(li[3][2])
li[1:3] = [1, 20, 32, 33]
print(li)
del li[1:3]
  • 元组
tu = (11, 22, 11, 33, 44,)
tu.count(11)      # 获取指定元素在元组中出现次数
v = tu.index(11)  # 获取指定元素在元组中第一个的位置
print(v)

tu = (111, 222, 333, )  # 元素不可被修改。

v = tu[0]

v = tu[0:2]

for item in range(0, 5):
    print(tu[item])

s = "djqwuidsdaf"
li = ["dgfhfsd", "djksd"]
tu = (123, 123, "asd")

v = list(tu)    #元组转换列表
  • 字典
info = {
    "k1": "v1",      # 键值对
    "k2": "v2"
}
v = dict.fromkeys([123, 'k1', 999], 123)  # 根据序列创建字典,附上固定值
print(v)

v = info.get('k1')    # 获取键值,key不存在,可以指定默认值
v = info.pop('k11', 90)    # 删除并获取值,如没有返回指定值
v = info.popitem()     # 随机删除项目
print(v)

v = info.setdefault('k1', '123')   # 设置值,若key存在,获取当前key对应的值。

info.update({'k1': '1111', 'k3': 123})
info.update(k1='1121', k3=123)       # 更新
print(info)


info = {            # 键值对键值的key必须是确定值,hashable转换存
    2: "asd",
    "k1": 'sda',
    "k4": 'ssa',
    "k2": "v1",      # 键值对
    "k3": [22, 33, 44],
    True: 'sda',
    (10, 20): 'sda'
}
print(info)

del info[1]  # 根据k值寻找操作

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

  • 集合
s = frozenset('hello')        # 不可变集合
s = {1, 1, 2, 3, 4, 5, '1'}   # 去重,集合
s.pop()
s.pop()
s.remove(4)
v = s.discard(11)
print(s, v)

Python_1 = {'alex', 'apple', 'orange', 'alex'}
linux_1 = {'alex', 'apple', 'banana'}
p_s = set(Python_1)
l_s = set(linux_1)
print(p_s, l_s)
print(p_s.intersection(l_s))   # 合集
print(p_s & l_s)

print(p_s.union(l_s))   # 并集
print(p_s | l_s)

print(p_s - l_s)       # 差集
print(l_s.difference(p_s))       # 差集
print(p_s.difference_update(l_s),p_s)

print(p_s.symmetric_difference(l_s)) # 交叉补集
print(p_s ^ l_s)

s1 = {1, 2}
s2 = {3, 5}
print(s1.isdisjoint(s2))

s1 = {1, 2}
s2 = {1, 4, 3}
print(s1.issubset(s2))
print(s2.issuperset(s1))
s1.update(s2)           # 更新多个值比union多个赋值add()只能传一个值
print(s1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值