序列
定义:
是指它的成员都是有序排列的,并且可以通过偏移量访问它的成员
不是python 中指定的数据类型,仅仅是多种类型所支持的统一操作
组成:
字符串、列表、元组等
支持的操作:
(1) 通过索引获取指定位置上的元素([]) – 序列[index_no]
one_str = "fdsghgfhfghg"
print(one_str[3])
运行结果:
g
(2) 切片操作([1:]) – 序列[数字:数字]
one_str = "fdsghgfhfghg"
print(one_str[3:7])
运行结果:
ghgf
(3) 成员关系操作符(in或者not in) – 对象[not] in 序列
one = "s"
one_str = "fdsghgfhfghg"
if one in one_str:
print("g在字符串中")
运行结果:
g在字符串中
```python
(4) 连接操作符(+) -- 序列+序列
one_str = “fdsghgfhfghg”
two_str = “demo”
one_list = [12,“demo”]
two_list = [“demo”,“lemon”]
print(one_str+two_str)
print(one_list+two_list)
print(one_str+one_list) # 注:序列+序列需要同类型不然会报错
运行结果:
```python
Traceback (most recent call last):
File "F:/py37/py16/demo_031801.py", line 173, in <module>
print(one_str+one_list) # 注:序列+序列需要同类型不然会报错
TypeError: can only concatenate str (not "list") to str
fdsghgfhfghgdemo
[12, 'demo', 'demo', 'lemon']
(5) 重复操作符(*) – 序列 * 数字
one_str = "fdsghgfhfghg"
one_list = [12,"demo"]
print(one_str*3)
print(one_list*3)
运行结果:
fdsghgfhfghgfdsghgfhfghgfdsghgfhfghg
[12, 'demo', 12, 'demo', 12, 'demo']
(6) 遍历操作(for list) – 语法:for item in 序列
one_list = [12,"demo","lemon","apple"]
for item in one_list:
print(item)
运行结果:
12
demo
lemon
apple
(7) 求长度(len) – len(序列)
one_str = "fdsghgfhfghg"
one_list = [12,"demo","lemon","apple"]
print(len(one_str))
print(len(one_list))
运行结果:
12
4
(8)内置函数:
8.1 all() 如果序列的所有元素都为True,则为True,否则为False.(空序列为True)
8.2 any() 序列只要有一个元素为True,则为True,否则为False(或者序列为None)
8.3 enumerate() 返回一个enumerate对象,返回一个包含序列每个元素索引和值的元组,it contains the index and value of all the itemss of list as a tuple
for item in enumerate(one_list):
print(item)
运行结果:
(0, 12)
(1, 'demo')
(2, 'lemon')
(3, 'apple')
8.4 len() 返回序列中元素的个数,return the length(the number of items) in the list
one_list = [12,"demo","lemon","apple"]
print(len(one_list))
运行结果:
4
8.5 list() 将一个可迭代对象转换为一个列表,convert an iteable(tuple,string,set,dicrionary) to a list
one_str = "fdsghgfhfghg"
print(list(one_str))
运行结果:
['f', 'd', 's', 'g', 'h', 'g', 'f', 'h', 'f', 'g', 'h', 'g']
8.6 max() 返回列表中最大的元素,return the largest item in the list
one_list = [12,13,63,23]
print(max(one_list))
运行结果:
63
拓展:
如何从序列中取出最大值
将列表[13, 20, 42, 85, 9, 45]中的最大值为85
提示:使用多种方法
方法一:
num_01 = [13,20,42,85,9,45]
print(max(num_01))
方法二:
num_02 = [13,20,42,85,9,45]
num_02.sort()
print(num_02[-1])
方法三:
num_03 = [13,20,42,85,9,45]
max = num_03[0]
for num in num_03:
if num>max:
max = num
print(max)
方法四;
num_04 = [13,20,42,85,9,45]
maxIndex = 0
for item in range(0,len(num_04)):
if num_04[item]>num_04[maxIndex]:
maxIndex = item
print(num_04[maxIndex])
8.7 min() 返回列表中最小的元素,return the smallest item in the list
num_01 = [13,20,42,85,9,45]
print(min(num_01))
8.8 sorted() 返回一个排序后的新序列(源序列不会被改变),return a new sorted list (does not sort the list itself)
8.9 sum() 返回序列中所有元素之和,(元素为数字) return the sum of all elements in the list
列表list
1.支持的操作
1.1 list.append(value) 只能在列表的末尾添加,每次只能添加一个值
1.2 list.extend(n_list) 把两个列表合并成一个列表,存到list中
userinfo = ["蓝天",18,"running"]
add_info = ["45kg",1.75]
like_colour = ["green","red"]
userinfo.append(add_info)
print(userinfo)
userinfo.extend(like_colour)
print(userinfo)
运行结果:
['蓝天', 18, 'running', ['45kg', 1.75]]
['蓝天', 18, 'running', ['45kg', 1.75], 'green', 'red']
注:append是将列表作为一个整体传入,extend是将列表拆分后传入
1.3 list.insert(i,value) 在列表的指定i位置,插入value值
userinfo = ["蓝天",18,"running"]
add_info = ["45kg",1.75]
userinfo.insert(2,add_info)
print(userinfo)
运行结果:
['蓝天', 18, ['45kg', 1.75], 'running']
1.4 list.pop(i) 删除指定位置的元素,如果不传i,默认删除末尾元素
userinfo = ["蓝天",18,"running","45kg",1.75]
userinfo.pop()
print(userinfo)
userinfo.pop(1)
print(userinfo)
运行结果:
['蓝天', 18, 'running', '45kg']
['蓝天', 'running', '45kg']
1.5 del list(i) 删除指定位置的元素
userinfo = ["蓝天",18,"running","45kg",1.75]
del userinfo[1]
print(userinfo)
运行结果:
['蓝天', 'running', '45kg', 1.75]
1.6 list.remove(value) 删除指定元素
userinfo = ["蓝天",18,"running","45kg",1.75]
userinfo.remove("running")
print(userinfo)
运行结果:
['蓝天', 18, '45kg', 1.75]
1.7 list.clear() 清空元素
userinfo = ["蓝天",18,"running","45kg",1.75]
userinfo.clear()
print(userinfo)
运行结果:
[]
1.8 list.sort() 对列表进行升序排列
num_01 = [13,20,42,85,9,45]
num_01.sort()
print(num_01)
运行结果:
[9, 13, 20, 42, 45, 85]
1.9 list.sort(reverse=True) 对列表进行降序排列
num_01 = [13,20,42,85,9,45]
num_01.sort(reverse=True)
print(num_01)
运行结果:
[85, 45, 42, 20, 13, 9]
1.10 list.count() 统计list元素的个数
num_01 = [13,20,13,42,85,9,45]
print(num_01.count(13))
运行结果:
2
1.11 list.reverse() 将元素进行翻转
num_01 = [13,20,13,42,85,9,45]
num_01.reverse()
print(num_01)
运行结果:
[45, 9, 85, 42, 13, 20, 13]
元组tuple
创建:
my_tuple1 = "hello","world"
my_tuple2 = (1,2,3)
my_tuple3 = ("a",)
my_tuple4 = ()
print("元组1:{}\n元组2:{}\n元组3:{}\n元组4:{}".format(my_tuple1,my_tuple2,my_tuple3,my_tuple4))
运行结果:
元组1:('hello', 'world')
元组2:(1, 2, 3)
元组3:('a',)
元组4:()