赞
踩
有顺序的,可以通过索引取值,可以遍历。
- # 1.列表拼接
- list1 = [1, 2, 3, 4, 5, 6]
-
- list2 = [7, 8, 9]
-
- list3 = list1 + list2
-
- print(list3) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
-
-
- # 2.元组拼接
-
- tup1 = (1, 2, 3, 4)
-
- tup2 = (5, 6, 7)
-
- tup3 = tup1 + tup2
-
- print(tup3) # (1, 2, 3, 4, 5, 6, 7)
-
-
- # 3.字符串拼接
-
- str1 = "Hello,"
-
- str2 = "World"
-
- str3 = str1 + str2
-
- print(str3) # Hello,World

- list1 = [1, 2, 3]
-
- tup1 = (1, 2, 3)
-
- str1 = 'abc'
-
- print(list1*2) # [1, 2, 3, 1, 2, 3]
-
- print(tup1*2) # (1, 2, 3, 1, 2, 3)
-
- print(str1*2) # abcabc
- # 以列表切片为例:
- list1 = [1, 2, 3, 4, 5, 6]
-
- print(id(list1))
-
- # 场景1: 正序取片,仅有起始索引,则从该索引开始取到最后且step为1
-
- list2 = list1[2:]
-
- print(list2) # [3,4,5,6]
-
- # 场景2: 正序取片,仅有结束索引,则从0开始索引开始取到该索引且step为1
-
- list3 = list1[:2]
-
- print(list3) # [1,2,3]
-
- # 场景3: 正序取片,3个参数从左向右取
- list4 = list1[1:4:2]
-
- print(list4) # [2, 4]
-
-
- # 场景4: 正序取片,无参数(默认从左向右取值,0开始索引到末尾索引,步长为1)
-
- list5 = list1[::]
-
- print(list5) # [1,2,3,4,5]
-
- # 逆序取片,3个参数从右向左取
- list6 = list1[3:1:-1]
-
- print(list6) # [4,3]
-
- # 逆序取片,仅有起始索引,则从该索引开始取到最后且step为-1
-
- list7 = list1[3::-1]
-
- print(list7) # [4,3,2,1]
-
- # 逆序取片,仅有结束索引,则从起始索引到该索引且step为-1
-
- list8 = list1[:2:-1]
-
- print(list8) # [6,5,4]
-
- # 逆序取片,仅有step且为-1
-
- list9 = list1[::-1]
-
- print(list9) # [6,5,4,3,2,1]
-
-

- 切片赋值
- lst = [1,2,3,4]
-
- lst[2:] = 0
-
- print(lst) # TypeError: can only assign an iterable
-
-
-
- str = '112113112114'
-
- print(str[3:1]) # 正向切片取不到值,因为从左到右取值起始索引晚于结束索引
-
- print(str[3:-1:-1]) # 逆向切片取不到值,因为从右到左取值起始索引晚于结束索引
- # 1.列表拼接
- list1 = [1, 2, 3, 4, 5, 6]
-
-
- # 取索引为0的第1个元素
- print(list1[0]) # 1
-
- # 取反索引的值即~索引=-(索引值+1)
- print(list1[~2]) # 4
-
-
- # 2.元组拼接
-
- tup1 = (1, 2, 3, 4)
-
- print(tup1[3]) # 4
-
-
- # 3.字符串拼接
-
- str1 = "Hello,"
-
- print(str1[2]) # l

- str1 = "hello world"
-
- print(str1.count('l') # 3
-
-
- list1 = [1,2,3,4,3,2,1]
-
- print(list1.count(3)) # 2
-
-
- tuple1 = (1,2,3,4,3,2,1)
-
- print(tuple1.count(3)) # 2
- 题目一: 计算字符串中第一个不重复的字符
-
- str = "Hi,Hello,World,hei"
-
- def fuc(str):
-
- for j in str:
-
- if str.count(j) == 1:
-
- return j
-
- return False
-
- print(fuc(str))
3.列表与元组的区别
区别 | 列表 | 元组 |
表示符号 | [] | {} |
序列 | 可变序列 | 不可变序列 |
访问和处理速度 | 动态数组,慢 | 静态数组,快 |
是否可作为字典的键 | 否 | 是 |
1. 有序序列逆序
- 方法一: 使用切片
-
- lst = [1, 2, 3, 4]
- str1 = "Hello"
- tup1 = (1, 2, 3, 4)
-
- print(lst[::-1])
- print(str1[::-1])
- print(tup1[::-1])
-
- 方法二: 使用reversed()内置函数
-
- lst = [1, 2, 3, 4]
- str1 = "Hello"
- tup1 = (1, 2, 3, 4)
- print(list(reversed(lst)))
- print("".join(list((reversed(str1)))))
- print(tuple(reversed(tup1)))
-
-
- 方法三: for和range()
-
- lst = [1, 2, 3, 4]
- str1 = "Hello"
- tup1 = (1, 2, 3, 4)
- lst1 = [lst[i] for i in range(len(lst)-1, -1, -1)]
- print(lst1)
- str2 = [str1[i] for i in range(len(str1)-1, -1, -1)]
- print("".join(str2))
- tup2 = [tup1[i] for i in range(len(tup1)-1, -1, -1)]
- print(tuple(tup2))
-
-
- 方法四: while
-
- lst = [1, 2, 3, 4]
- str1 = "Hello"
- tup1 = (1, 2, 3, 4)
- i = len(lst)-1
- while i >= 0:
- print(lst[i])
- i -= 1
-
-
-
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。