0
点赞
收藏
分享

微信扫一扫

DAY3 数据类型-字符串&列表

1.字符串定义

  • 字符串是由零个或多个字符组成的有限序列
  • 不可变(hashmap)、有索引、可切片、可遍历

2.一切字符串皆对象

3.字符串类型方法

  • 格式化方法
  • 判断方法
  • 查、改、计数、替换
  • 特殊变态方法
  • 帮助文档help(a.expandtabs)

字符串可用方法:a为一个字符串变量

a.capitalize (首字母大写)
a.casefold (为了方便字符串之前对比,都改成小写)
a.center (字符串两边填充) e.g.: a.center(50,'-')
a.expandtabs (tab默认8个字符(空格),可以指定多个字符大小)
a.ljust (从字符串左边开始不够右边填充)
a.rjust (从字符串右边开始不够左边填充)
a.lower (全变小写)
a.swapcese (大小写互换)
a.title (每个首字母大写)
a.upper (改大写)
a.strip (两边去空格、无用字符) 用的比较多
a.lstrip (左边去空格、无用字符)
a.rstrip (右边去空格、无用字符)
a.format (引用外部变量)


'{}{}{}.xls'.format(month_day, distict, ascription)

print(f'my name is {s}')

>>> msg = "my name is {name}, i am {age} years old."
>>> msg.format(name="yngwie",age=33)
'my name is yngwie, i am 33 years old.'

>>> msg = "my name is {1}, i am {2} years old."
>>> msg.format("yngwie",33)
'my name is yngwie, i am 33 years old.'

字符串判断

>>> s.startswith("")以什么开头
>>> s.endswith("")以什么结尾
>>> s.isalnum() 是不是字母or数字
>>> s.isalpha() 是不是字母
>>> s.isdight() 是不是数字(只能判断整数)
>>> s.isidentifier() 是不是合法的可以做变更的名字
>>> s.islower() 是不是小写
>>> s.isnumeric() 是数字形式(中文、英文、数字都可以)
>>> s.lsprintable 是否可打印
>>> s.isspace 是不是空格
>>> s.isupper() 是不是大写

字符串查找&计数&修改&替换

a.find("") (找到返回索引,找不到返回-1
a.rfind("")
a.index("") 查索引(找到返回索引,找不到报错)
a.count("") 计数
name.split() 切分成列表(默认是以空格为分隔符,可以在()中添加分隔符)
name.splitlines()
>>> msg = "hello,\nerveryone\n haha\nddd"
>>> msg.splitlines()
["hello","erveryone","ddd"]

name.replace() 把什么替换成什么
>>> name = "The First Chaiman In China"
>>> name
'The First Chaiman In China'
>>> name.replace('China','Vietnam')
'The First Chaiman In Vietnam'
>>>

1.5 特殊变态方法

用‘-’链接列表中的元素
>>> names = ["Monkey","Celina"]
>>>"-".join(names)
'Monkey-Celina'

开发一个文本加密小程序

a.maketrans 生成密码本
a.translate 加密

>>> source = "abcdefghi"
>>> output = "012345678"

>>> str.maketrans(source,output)
{97: 48, 98: 49, 99: 50, 100: 51, 101: 52, 102: 53, 103: 54, 104: 55, 105: 56}
>>> password_table = str.maketrans(source,output)

>>> msg = 'hello baby'
>>> msg.translate(password_table)
'74llo 101y'

>>> import string #自带模块
>>> string.ascii_letters #大小写
>>> string.printtable #大小写 数字 特殊符号
>>> string.printtable[::-1] #反转 列表切片
>>> source2 = string.printtable
>>> output2 = string.printtable[::-1]
>>> password_table2 = str.maketrans(source2,output2)
>>> msg = 'Hello baby, I miss you so much, want to see you, but afraid the female tiger find out...'
>>> encrpted_msg = msg.translate(password_table2)
解密:把正向反的密码本
>>> password_table3 = str.maketrans(output2,source2)
>>> encrpted_msg.translate(password_table3)

统计字符、数字、特殊字符的个数

while True:
msg = input(">:").strip()
if not msg:
continue
# 'Hello baby, I miss you so much, want to see you, but afraid the female tiger find out...'
str_count = 0
int_count = 0
space_count = 0
special_count = 0
for i in msg:
if i.isalpha():
str_count += 1
elif i.isdigit():
int_count += 1
elif i.isspace():
space_count += 1
else:
special_count += 1

print(f"str count:{str_count},int count:{int_count},space count: {space_count},special count: {special_count}")

三、列表精讲

列表特性:

1.有序

2.有索引、可切片、可遍历

增:

name.append()

name.insert()

删除:

name.clear()   清空列表

name.pop()     默认删除最后一个,并返回删除的数,可指定索引

name.remove("")  删除指定的值,从列表中找到最左边相应的数值

del name   删除列表

改:

>>> li = [0,1,2,3,4,5,6,7,8,9]
>>> li[2]
2
>>> li[2] = 'A'
>>> li
[0, 1, 'A', 3, 4, 5, 6, 7, 8, 9]
>>>

切片

正切 :注意切片时不包括最后一个序号

>>> li[0:5]
[0, 1, 'A', 3, 4]
>>> li[0:]
[0, 1, 'A', 3, 4, 5, 6, 7, 8, 9]
>>>li[:]
[0, 1, 'A', 3, 4, 5, 6, 7, 8, 9]
>>> a = '1234.txt'
>>> a[-4:] #取出文件后缀
>>> '.txt'

步长:[0:8:2]的意思是从第0个字符到第8个字符每2个取一次

>>> li[0:8:2]
[0, 'A', 4, 6]
>>>>>> li[::2]
[0, 'A', 4, 6, 8]
>>> li[::3]
[0, 3, 6, 9]
>>>

反转:

li.reverse()

>>> li
[0, 1, 'A', 3, 4, 5, 6, 7, 8, 9]
>>> li[::-1] # -1是步长
[9, 8, 7, 6, 5, 4, 3, 'A', 1, 0]
>>> li[0:9:-1]
[]
>>> li[9:0:1]
[]
>>> li[9:0:-1]
[9, 8, 7, 6, 5, 4, 3, 'A', 1]
>>>

查:

>>> li
[0, 1, 'A', 3, 4, 5, 6, 7, 8, 9]
>>> 'a' in li
False
>>> 'A' in li
True
>>>
>>> li.count('A') #记数
1
>>> li.index('A')
2
>>>

特殊:

names.reverse()  #反转
names.sort() #排序
names.extend() #把另一个列表并在li这个列表中
# e.g. a = [1,3,5,6,7,2,22] li3 = li + a 会得到一个新的列表需要赋值

names.copy() #浅copy shallow copy如下只能copy一层,内嵌的列表无法copy

>>> a = [1,2,3,4,['Alex','Jack'],6,7]
>>> b = a.copy()
>>> b
[1, 2, 3, 4, ['Alex', 'Jack'], 6, 7]
>>> a
[1, 2, 3, 4, ['Alex', 'Jack'], 6, 7]
>>> id(a)
39259968
>>> id(b)
39274432
>>>
#看似复制,其实是同一块内存中的数据
>>> id(a[0])
8791034812064
>>> id(b[0])
8791034812064
>>>
>>> id(b[4])
39265152
>>> id(a[4])
39265152
>>> b[4]
['Alex', 'Jack']
>>>

深copy #Deep copy 完全复制,需要用到copy模块。和列表的copy功能有区别
>>> import copy
>>> a
[1, 2, 3, 4, ['Alex', 'Jack'], 6, 7]
>>> b
[1, 2, 3, 4, ['Alex', 'Jack'], 6, 7]
>>> c = copy.deepcopy(a)
>>> c
[1, 2, 3, 4, ['Alex', 'Jack'], 6, 7]
>>> a[4][1] = 'Black girl'
>>> a
[1, 2, 3, 4, ['Alex', 'Black girl'], 6, 7]
>>> b
[1, 2, 3, 4, ['Alex', 'Black girl'], 6, 7]
>>> c
[1, 2, 3, 4, ['Alex', 'Jack'], 6, 7]
>>>

列表生成式

staff_list = []
for i in range(1,31):
staff_list.append(f"staff-{i}")
print(staff_list)

只有一行代码时可以写成一行
staff_list = [ f"staff-{i}" for i in range(1,30) ]





举报

相关推荐

0 条评论