字符串
5. 字符串的对象方法
字符串.函数名()
- 字符串1.count(字符串2) -> 统计字符串1中字符串2出现的次数
print('zkgakaahjka'.count('a'))
- 字符串1.find(字符串2) -> 返回字符串2在字符串1中第一次出现的下标(0~长度-1的下标);找不到返回-1
str1 = 'how are you!fine,and you'
print(str1.find('you'))
- 字符串1.find(字符串2,startindex,endindex)
print(str1.find('you', 10, 15))
- 字符串.join(序列) -- >将序列中的元素取出来,中间用指定字符串拼接在一起产生一个新的字符串
new_str = '*'.join('abc')
print(new_str)
new_str = ','.join(['name', 'age', 'xiaoming']) # 元素必须是字符串
print(new_str)
- max和min
max(序列),min(序列)
print(max('aehafgeusjazaui雒'))
print(min('hahfiufffiuea1'))
print(max(range(100)))
- 字符串.replace(old,new,替换次数) -- 将字符串中指定的旧字符串全都替换成新的字符串
str1 = 'how are you!fine,and you'
new_str = str1.replace('you', 'You', 1)
print(new_str)
- 字符串1.split(字符串2) -- 将字符串1按照字符串2进行切割,返回的是列表
str1 = 'how are you ! fine , and you'
result = str1.split(' ')
print(result)
列表(list)
1. 什么是列表(list)
列表是python提供的容器型数据类型(序列),可变、有序的
- 可变(元素的个数、值和循序可变) -- 支持增删改
- 有序 -- 支持下标操作
2. 列表数据:[元素1,元素2,元素3,...]
元素:
- 可以是任何类型的数据(值、赋值后的变量、除了赋值符以外的其他运算表达式)
- 不同的元素的类型可以不一样
list1 = [12, 23.1, True, 'asd', [1, 2], (1, 2), {'a': 10}, lambda x: x * 2]
print(list1)
num = 10
list2 = [num, num*10+2, 'abc'.count('a')]
print(list2)
3. 列表元素的操作
3.1 查 -- 获取列表中的元素
1)获取单个元素
语法:列表[下标] -- 获取列表中指定下标对应的元素(返回值就是对应的元素)
注意:下标不能越界
names = ['路飞', '鸣人', '佐助', '索罗', 100]
print(names[1])
print(names[-1])
# print(names[10]) # IndexError: list index out of range
2)获取部分元素(切片) -- 结果是列表
列表[开始下标:结束下标:步长]
print(names[1:-1]) # ['鸣人', '佐助', '索罗']
list2 = names[-1:1] # [ ]
print(list2)
list2 = names[-1:1:-1]
print(list2) # [100, '索罗', '佐助']
print(names[:3]) # ['路飞', '鸣人', '佐助']
print(names[:3:-1]) # [100]
print(names[:]) # 相当复制 ['路飞', '鸣人', '佐助', '索罗', 100]
print(names[::-1]) # 倒着取[100, '索罗', '佐助', '鸣人', '路飞']
3)遍历
for 变量 in 列表:
循环体
变量直接取到的是列表中的每个元素(item -> 元素;index -> 下标)
# 直接遍历元素
names = ['路飞', '鸣人', '佐助', '索罗']
for name in names:
print(name)
# 遍历下标
for index in range(len(names)):
print(names[index])
补充:isinstance函数
isinstance(数据,类型) --判断指定的数据是否是指定的类型
print(isinstance(100.0, int))
- 练习:统计一个列表中整形元素的个数
list3 = [23, 78.2, 'adf', [12, 3], 290]
count = 0
for item in list3:
if isinstance(item, int):
count += 1
print(count)
3.2 增 -- 增加元素
1)列表.append(元素) -- 在列表的最后添加一个元素(修改原列表,不会产生新的列表)
films = ['复联4', '指环王', '绿皮书', '你的名字', '千与千寻']
films.append('肖生克的救赎')
print(films)
2)列表.insert(下标,元素) -- 在列表中指定下标前添加指定元素
films.insert(2, '沉默的羔羊')
print(films)
- 练习1:有一个有序的数字列表,输入一个数,将这个数插入到列表中,要求插入后不改变排列方式
nums = [1, 12, 32, 45, 60]
num = int(input('请输入一个数:'))
for index in range(len(nums)):
if nums[index] >= num:
nums.insert(index, num)
break
else:
nums.append(num)
print(nums)
3.3 删 -- 删除列表中的元素
1)del 列表[下标] -- 删除列表中指定下标对应的元素
films = ['复联4', '指环王', '绿皮书', '你的名字', '千与千寻', '肖生克的救赎']
del films[1]
print(films)
del films[-1]
print(films)
# del films[100] # IndexError: list assignment index out of range
2)列表.remove(元素) -- 删除列表中指定的元素
注意:1.如果需要删除的元素在列表中有多个,只删最前面的一个
2.如果要删除的元素不存在程序会报错!
films = ['复联4', '指环王', '绿皮书', '你的名字', '千与千寻', '肖生克的救赎', '指环王']
# films.remove('指环王2') # ValueError: list.remove(x): x not in list
films.remove('指环王')
print(films)
3)列表.pop()
列表.pop() -- 取出列表中最后一个元素
列表.pop(下标) -- 取出列表中指定下标对应的元素
films = ['复联4', '指环王', '绿皮书', '你的名字', '千与千寻', '肖生克的救赎', '指环王']
del_value = films.pop()
print(films, del_value)
del_value = films.pop(0)
print(films, del_value)
- 练习:删除列表中成绩低于60的所有成绩
错误情况:
scores = [70, 45, 50, 87, 67, 30, 100]
for index in range(len(scores)):
if scores[index] < 60:
del scores[index]
print(scores)
坑1: 下标越界
scores = [70, 45, 50, 87, 67, 30, 100]
index = 0 ~ 6
index = 0; 70 < 60
index = 1; 45 < 60; del scores[1]; scores = [70, 50, 87, 67, 30, 100]
index = 2: 87 < 60
index = 3: 76 < 60
index = 4: 30 < 60; del 4 ; scores = [70, 50, 87, 67, 100]
index = 5: scores[5] -> 崩溃
解决坑1:下标从后往前取
scores = [70, 45, 50, 87, 67, 30, 100]
index = 6 ~ 0
index = 6; 100 < 60
index = 5; 30 < 60; [70, 45, 50, 87, 67, 100]
index = 4; 67 < 60
index = 3; 87 < 60
index= 2; 50 < 60; [70, 45, 87, 67, 100]
index =1; 45 < 60; [70, 87, 67, 100]
index =0; 70 < 60
scores = [70, 45, 50, 87, 67, 30, 100]
for index in range(len(scores) - 1, -1, -1):
if scores[index] < 60:
del scores[index]
print(scores)
解决坑1:使用while循环控制下标在不删除的时候才增加1,删除的时候不变
scores = [70, 45, 50, 87, 67, 30, 100]
index = 0: 70 < 60
index = 1: 45 < 60 [70, 50, 87, 67, 30, 100]
index = 1: 50 < 60 [70, 87, 67, 30, 100]
index = 1: 87 < 60
index = 2: 67 < 60
index = 3: 30 < 60 [70, 87, 67, 100]
index = 3: 100< 60
scores = [70, 45, 50, 87, 67, 30, 100]
index = 0
while index < len(scores):
if scores[index] < 60:
del scores[index]
continue
index += 1
print(scores)
坑2:直接获取列表元素
错误:
scores = [70, 45, 50, 87, 67, 30, 100]
for score in scores:
if score < 60:
scores.remove(score)
print(scores)
scores = [70, 45, 50, 87, 67, 30, 100]
(第一)score = 70
(第二)score = 45; 45 < 60; [70, 50, 87, 67, 30, 100]
(第三)score = 87;
(第4)score = 67;
(第5)score = 30; 30<60; [70, 50, 87, 67, 100]
(第6)
解决坑2
scores = [70, 45, 50, 87, 67, 30, 100]
for score in scores[:]:
if score < 60:
scores.remove(score)
print(scores)
注意:遍历删除的时候需要考虑这两个坑!
3.4 改 - 修改元素的值
列表[下标] = 新值 - 修改指定下标对应的元素为新值
注意: 下标不能越界!
balls = ['乒乓球', '篮球', '足球', '排球']
balls[1] = '羽毛球'
print(balls)
- 练习:将低于60分的元素改成不及格
scores = [70, 45, 50, 87, 67, 30, 100]
for index in range(len(scores)):
if scores[index] < 60:
scores[index] = '不及格'
print(scores)
4.列表运算符和列表的对象方法
- 列表运算符
1)数学运算: +, *
- 列表1+列表2 -> 将两个列表中的元素合并产生一个新的列表
new_list = ['a', 'b', 'c'] + [1, 2, 3]
print(new_list)
- 列表1 * N -> 列表中的元素重复N次产生一个新的列表
print([1, 2, 3] * 3)
2)比较运算: ==, !=
print([1, 2, 3] == [1, 3, 2]) # False
print([1, 2, 3] == [1, 2, 3]) # True
print({1, 2, 3} == {1, 3, 2}) # True
- in /not in
元素 in 列表 --> 判断列表中是否包含指定的元素
print([1, 2] in [1, 2, 3, 4, 5]) # False
print([1, 2] in [1, 2, 3, 4, 5, [1, 2]]) # True
- len(列表) --> 获取列表中元素的个数
- list(数据) --> 将指定数据转换成列表
所有的序列都可以转换成列表;将序列中的元素作为列表元素
# print(list(200)) # TypeError: 'int' object is not iterable
print(list('hello world'))
print(list(range(20, 32)))
5.max/min
max(列表) -- 获取列表元素中的最大值
min(列表) -- 获取列表元素中的最小值
注意:1.列表中元素的类型必须一致 2.数据支持比较运算符
print(max([129, 45, 3, 56]))