str
用途:记录描述性的状态,比如人的名字、地址、性别 定义方式:在 '' , "" , """""" 内包含一系列的字符
常用操作及内置方法
-
按索引取值
name = 'lcy' print(name[0]) # l print(name[-1]) # y
-
切片(顾头不顾尾,步长)
name = 'lcy' print(name[0:1]) # l print(name[-1:-4:-1]) # ycl
-
len(长度)
name = 'lcy' print(len(name)) # 3
-
成员运算in和not in
判断一个子字符串是否存在于一个大的字符串中
name = 'lcy' print('c' in name) # True print('b' not in name) # True
-
strip(lstrip,rstrip)
去掉字符串左右两边的字符,不管中间的
test = 'aabbaabbaa' print(test.strip('a')) # bbaabb
-
split
针对按照某种分隔符组织的字符串,可以用split将其切分成列表,进而进行取值
test = 'aabb/aabb/aa' print(test.split('/')) # ['aabb', 'aabb', 'aa']
-
循环
name = 'lcy' for letter in name: print(letter) # l c y
-
lower和upper
name = 'Lcy' print(name.lower()) # lcy print(name.upper()) # LCY
-
startswith和endswith
name = 'Lcy' print(name.startswith('L')) # True print(name.endswith('y')) # True
-
format的三种玩法
test = '{} {} {}'.format('lcy', 18, 'male')
test = '{0} {1} {2}'.format('lcy', 18, 'male')
test = '{name} {age} {sex}'.format(sex='male', name='lcy', age=18)
print(test)
-
join
将字符串作为拼接符 把列表中的元素拼接成一个大的字符串
test = '0' test_list = ['1', '2', '3'] print(test.join(test_list))
-
replace
将字符串中的某个字符 替换为另一个字符
test = 'aabbaabbaa' print(test.replace('b', 'W', 3)) #aaWWaaWbaa
-
isdigit,isalpha,isalnum
isdigit:字符串中包含的是数字
isalpha:字符串中包含的是字母或者中文字符
isalnum:字符串中包含的是字母(中文字符)或数字
test = '123' print(test.isdigit()) #True test = 'abc中国' print(test.isalpha()) #True test = 'abc123,中国' print(test.isalnum()) #False
-
find,rfind,index,rindex(取索引)
test = 'abc123,中国' print(test.find('c')) #2 print(test.find('d')) #-1 print(test.index('c')) #2 print(test.index('d')) #报错
-
count
test = 'abbc123,中国' print(test.count('b')) #2
-
center,ljust,rjust,zfill(填充)
test = '123' print(test.center(10, '*')) # ***123**** print(test.ljust(10, '*')) # 123******* print(test.zfill(10)) # 0000000123
-
expandtabs
默认占八位 expandtabs将 转化成指定位数的空格 默认8
test = '123 4' print(test) #123 4 print(test.expandtabs()) # 123 4 print(test.expandtabs(16)) # 123 4
类型总结
- 存一个值
- 有序
- 不可变
list
用途:存放多个值,可以根据索引存取值 定义方式:在[]内用逗号分割开多个任意类型的值
常用操作及内置方法
-
按索引存取值
test = ['a', 'b', 1, 2] print(test[0]) # a test[3] = 3 # 超出范围报错 print(test) # ['a', 'b', 1, 3]
-
切片(顾头不顾尾 步长)
test = ['a', 'b', 1, 2] print(test[0:3]) #['a', 'b', 1] print(test[0:3:2]) #['a', 1]
-
len(长度)
test = ['a', 'b', 1, 2] print(len(test)) # 4
-
成员运算in和not in
test = ['a', 'b', 1, 2] print('a' in test) #True print('c' not in test) #True
-
append(追加)
test = ['a', 'b', 1, 2] test.append('c') print(test) #['a', 'b', 1, 2, 'c']
-
insert(往指定索引前插入值)
test = ['a', 'b', 1, 2] test.insert(2, 0) print(test) # ['a', 'b', 0, 1, 2]
-
删除
test = ['a', 'b', 1, 2, 1] del test[0] # 按索引删 print(test) # ['b', 1, 2, 1] test.remove('a') # 按元素取 print(test) # ['b', 1, 2, 1] res = test.pop() # 按索引删 默认删末尾 print(res) # 1 print(test) # ['a', 'b', 1, 2]
-
循环
test = ['a', 'b', 1, 2, 1] for element in test: print(element) # 'a' 'b' 1 2 1
-
count
test = ['a', 'b', 1, 2, 1] print(test.count('a')) # 1
-
index
test = ['a', 'b', 1, 2, 1] print(test.index('a')) # 0
-
clear
test = ['a', 'b', 1, 2, 1] test.clear() print(test) # []
-
extend
test1 = ['a', 'b', 'c'] test2 = [1, 2, 3] test1.extend(test2) print(test1) # ['a', 'b', 'c', 1, 2, 3] print(test2) # [1, 2, 3]
-
reverse(反序)
test = [1, 3, 5, 2, 4, 6] test.reverse() print(test) #[6, 4, 2, 5, 3, 1]
-
sort
test = [1, 3, 5, 2, 4, 6] test.sort(reverse=True) print(test) #[6, 5, 4, 3, 2, 1]
类型总结
- 存多个值
- 有序
- 可变
tuple
用途:记录多个值,当多个值没有改的需求,此时用元组更合适 定义方式:在()内用逗号分隔开多个任意类型的值
常用操作及内置方法
-
按索引取值(只能取)
test = (1, 2, 3, 'a', 'b', 'c') print(test[3]) # 'a'
-
切片(顾头不顾尾,步长)
test = (1, 2, 3, 'a', 'b', 'c') print(test[1:5:2]) # (2, 'a')
-
len(长度)
test = (1, 2, 3, 'a', 'b', 'c') print(len(test)) # 6
-
成员运算in和not in
test = (1, 2, 3, 'a', 'b', 'c') print(4 in test) # False print(4 not in test) # True
-
循环
test = (1, 2, 3, 'a', 'b', 'c') for element in test: print(element) # 1 2 3 'a' 'b' 'c'
-
count
test = ('a', 'b', 'b', 'c', 'c', 'c') print(test.count('b')) # 2
-
index
test = ('a', 'b', 'b', 'c', 'c', 'c') print(test.index('b')) # 1
类型总结
- 存多个值
- 有序
- 不可变
dict
用途:记录多个值,每一个值都对应的key用来描述value的作用 定义方式:在{}内用逗号分隔开多 个key:value,其中value可以是任意类型,而key必须是不可变的类型,
通常情况下应该str类型
生成字典的三种方式
-
方式一
dic = dict(x=1, y=2, z=3) print(dic) # {'z': 3, 'y': 2, 'x': 1}
-
方式二&三
user_info = [ ['name', 'lcy'], ['age', 18], ['sex', 'male'] ] dic = {} for key, value in user_info: dic[key] = value print(dic) # {'sex': 'male', 'age': 18, 'name': 'lcy'} dic = dict(user_info) print(dic) # {'name': 'lcy', 'sex': 'male', 'age': 18}
常用操作及内置方法
-
按key存取值
test = {'a': 1, 'b': 2, 'c': 3} print(test['a']) # 1 test['d'] = 4 print(test) # {'a': 1, 'c': 3, 'd': 4, 'b': 2}
-
len(长度)
test = {'a': 1, 'b': 2, 'c': 3} print(len(test)) #3
-
成员运算in和not in(判断的是key)
test = {'a': 1, 'b': 2, 'c': 3} print('d' in test) # False print('d' not in test) # True
-
删除
test = {'a': 1, 'b': 2, 'c': 3} del test['a'] print(test) #{'b': 2, 'c': 3} res = test.pop('a') print(res) # 1 print(test) #{'b': 2, 'c': 3}
-
键keys(),值values(),键值对items()
test = {'a': 1, 'b': 2, 'c': 3} print(list(test.keys())) #['b', 'a', 'c'] print(list(test.values())) # [2, 1, 3] print(list(test.items())) # [('b', 2), ('a', 1), ('c', 3)]
-
循环
test = {'a': 1, 'b': 2, 'c': 3} for element in test: print(element) # 'a' 'b' 'c'
-
fromkeys
test = ['name', 'age', 'sex'] dic = {} dic = dic.fromkeys(test, None) print(dic) # {'name': None, 'sex': None, 'age': None}
-
update
test1 = {'a': 1, 'b': 2, 'c': 3} test2 = {'c': 5, 'd': 4} test1.update(test2) print(test1) # {'b': 2, 'c': 5, 'a': 1, 'd': 4}
-
setdefault
有则不动,返回原值;无则添加,返回新值
test = {'a': 1, 'b': 2, 'c': 3} res = test.setdefault('b', 4) print(res) # 2 print(test) # {'a': 1, 'b': 2, 'c': 3} res = test.setdefault('d', 4) print(res) # 4 print(test) # {'d': 4, 'b': 2, 'c': 3, 'a': 1}
类型总结
- 存多个值
- 无序
- 可变
set
用途: 关系运算,去重 定义方式: 在{}内用逗号分开个的多个值
集合特性
- 每一个值都必须是不可变类型
- 元素不能重复
- 集合内元素无序
###常用操作及内置方法
-
交集 : & 或 intersection
test1 = {1, 2, 3, 'a', 'b'} test2 = {'a', 'b', 'c', 1, 2} print(test1 & test2) # {1, 2, 'b', 'a'} print(test1.intersection(test2)) # {1, 2, 'b', 'a'}
-
并集 : | 或 union
test1 = {1, 2, 3, 'a', 'b'} test2 = {'a', 'b', 'c', 1, 2} print(test1 | test2) # {1, 2, 3, 'b', 'c', 'a'} print(test1.union(test2)) # {1, 2, 3, 'b', 'c', 'a'}
-
差集 : - 或 difference
test1 = {1, 2, 3, 'a', 'b'} test2 = {'a', 'b', 'c', 1, 2} print(test1 - test2) # {3} print(test1.difference(test2)) # {3} print(test2 - test1) #{'c'} print(test2.difference(test1)) #{'c'}
-
对称差集 : ^ 或 symmetric_difference
test1 = {1, 2, 3, 'a', 'b'} test2 = {'a', 'b', 'c', 1, 2} print(test1 ^ test2) # {3, 'c'} print(test1.symmetric_difference(test2)) 3 {3, 'c'}
-
add
test = {1, 2, 3, 'a', 'b'} test.add('c') print(test) # {1, 2, 3, 'a', 'c', 'b'}
-
删除
test = {1, 2, 3, 'a', 'b'} test.pop() #随机删除 print(test) test.remove('a') #删除的值不存在 报错 print(test) #{1, 2, 3, 'b'} test.discard('a') #删除的值不存在 报错 print(test) #{1, 2, 3, 'b'}
-
update
test1 = {1, 2, 3, 'a', 'b'} test2 = {'a', 'b', 'c', 1, 2} test1.update(test2) print(test1) # {1, 2, 3, 'c', 'a', 'b'}
-
isdisjoint
如果两个集合没有交集则返回True
test1 = {1, 2, 3, 'a', 'b'} test2 = {'a', 'b', 'c', 1, 2} res = test1.isdisjoint(test2) print(res)
类型总结
- 存多个值
- 无序
- 可变
集合去重局限性
- 无法保证原数据类型的顺序
- 当某一个数据中包含的多个值全部为不可变的类型时才能用集合去重