第六节 序列类型详解

1. 列表生成式

第一种方法:

直接写

第二种方法:

range

第三种方法:

列表生成式,可以在列表中写for循环和if语句

语法:

[i for i in range()]

# 例: [1x1,2x2,3x3,......100x100]
list1 = [i * i for i in range(1, 101)]
print(list1)


[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801, 10000]
# 例:[1x2,2x3,3x4,......,9x10]
list2 = [i * (i + 1) for i in range(1, 10)]
print(list2)


[2, 6, 12, 20, 30, 42, 56, 72, 90]
# 求列表中长度大于3 的字符串
s = ['abc', 'jhikl', 'jk', 'n']
list3 = [i for i in s if len(i) > 3]
print(list3)


['jhikl']

2. 可变与不可变数据类型

可变类型:列表,字典,集合

不可变类型:数字,字符串,元组

可变(列表)

s = [1, 2, 3, 4]
s[3] = 'a'
print(s)


[1, 2, 3, 'a']

 不可变(元组)

s = (1, 2, 3)
s[0] = 4
print(s)



TypeError: 'tuple' object does not support item assignment

3. 字符串常用方法

3.1 初始方法

语法:

字符串.方法名()

3.2 find()

作用:可在指定范围查找子串,找到,返回这个子串的索引值,否则返回-1(用于检测字符串中是否包含子字符串str)

语法:

字符串.find(str,start,end)

str---->指定检索的字符串

start--->开始下标,默认为0.(start=0)

end--->结束下标,默认为字符串长度。(end=len(字符))

s = 'hello,world'
print(s.find('h'))   #h的下标就是0
print(s.find('world'))     #找到就会返回首个字符的下标
print(s.find('world '))     #返回-1



0
6
-1
s = 'hello,world,he,she'
print(s.find('h'))   #只会返回首个h
print(s.find('h',1))  # 1代表从下标为1的这个字母开始找h
print(s.find('h',13))   # 13代表从下标为13的这个字母开始找h
print(s.rfind('h'))

0
12
16
16

rfind则是从后往前找

3.3 index()

index()和find一样,唯一区别是:find找不到返回-1,index找不到则直接报错

语法:

字符串.index(str,beg,end)

3.4 count()

作用:返回子串找到的个数。

语法:

字符串.count(str)

s = '爬虫爬虫蟒蛇爬虫'
print(s.count('爬'))   # 返回指定元素出现的次数


3

3.5 lower()和upper()

lower():转小写

upper():转大写

s = 'hello,world,python'
a = s.upper()
print(a)
d = a.lower()
print(d)


HELLO,WORLD,PYTHON
hello,world,python

3.6 split()

切分字符串,将字符串转列表,默认以空格切分,也可以指定字符切分。

s = 'hello,world,python'
print(s.split(','))   #结果一定是列表
print(s.split('r')) 



['hello', 'world', 'python']
['hello,wo', 'ld,python']   

3.7 replace()

把字符串中的子串替换成新的字符串

str.replace(old,new,max)

old---->要替换的字符串

new---->新字符串

max---->替换次数(默认替换所有)

s = 'hello,world,python'
a = s.replace('h', '新') #替换的不是原字符串,因为字符串是不可被替换的,生成的是替换后的新字符串。
print(a)


新ello,world,pyt新on

3.8 join()

用于将序列中的元素以指定字符串链接成一个新的字符串。

常将列表类型转为字符串。

s = ['hello', 'world', 'python']
print(''.join(s))
s = ['hello', 'world', 'python']
print('~'.join(s))


helloworldpython
hello~world~python

4.列表的常用方法

4.1 append()

添加一个数据,添加到列表的最后一位。

语法:

列表名.append(内容)

s = ['hello', 'world', 'python']
s.append('爬虫')
print(s)


['hello', 'world', 'python', '爬虫']

4.2 insert()

添加一个数据,添加到指定位置。

语法:

列表名.insert(下标位置,内容)

s = ['hello', 'world', 'python']
s.insert(2,'爬虫')
print(s)


['hello', 'world', '爬虫', 'python']

4.3 extend()

添加一个序列类型,到最后一位,并且把序列类型拆分。

语法:

列表名.extend(序列类型)

s = ['hello', 'world', 'python']
s.extend('爬虫和蟒蛇')
print(s)


['hello', 'world', 'python', '爬', '虫', '和', '蟒', '蛇']

4.4 pop()

删除一个值,默认从最后一个删,也可以指定一个位置。

列表名.pop()

列表名.pop(下标)

s = ['hello', 'world', 'python']
s.pop()     #默认删除最后一个
print(s)



['hello', 'world']




s = ['hello', 'world', 'python']
s.pop(1)     #删除 下标为1 的
print(s)


['hello', 'python']

4.5 remove()

删除一个指定的值,如有多个,从第一个开始删除。

语法:

列表名.remove(删除对象)

s = ['hello', 'world', 'python']
s.remove('world')
print(s)


['hello', 'python']

4.6 clear()

清空列表

语法:

列表名.clear()

s = ['hello', 'world', 'python']
s.clear()
print(s)


[]

4.7 改

4.7.1单个修改

语法:

列表名[下标]=内容

s = ['hello', 'world', 'python']
s[-1] = '爬虫'
print(s)


['hello', 'world', '爬虫']

4.7.2 多个修改

语法:

列表名[起点:终点]=数据1,数据2.......

s = ['hello', 'world', 'python']
s[0:2] = 1, 2, 3
print(s)



[1, 2, 3, 'python']

4.8 查

4.8.1 index()

语法:

列表名.index()      #下标

4.8.2  count()

语法:

列表名.count()     #出现的次数

4.9  其它

4.9.1 sort()

让列表的内容按照升序或降序的方式进行排列。(只能排数字)

语法:

列表名.sort()  ---->默认是升序

s = [3, 4, 2, 5, 1, 9]
s.sort()   #默认是升序
print(s)
s.sort(reverse=True)    #降序
print(s)


[1, 2, 3, 4, 5, 9]
[9, 5, 4, 3, 2, 1]

5.元组的查

元组只能查,元组不能修改。

语法:

列表名.index()      #下标

列表名.count()     #出现的次数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dull_Demon_King

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值