Python list and tuple knowledge points

1. List

  • Lists can be indexed, sliced, and strided
li = [100, 'amw', True, 6668, 'kvi', [1, 'abc', 90, '12a'], False]
# 索引
print(li[0])
print(li[5][1])
# 切片,遵循“顾头不顾腚”原则
print(li[:3])
print(li[2:5])
print(li[-3:-6:-1])
print(li[::-2])
# 运行结果
100
abc
[100, 'amw', True]
[True, 6668, 'kvi']
['kvi', 6668, True]
[False, 'kvi', True, 100]

List creation

  • method one:
li = [1, 2, 'abc']
  • Method two:
li = list()             # 创建一个空列表
li = list('amwkvi')     # 创建列表内容为['a','m','w','k','v','I']
  • Method 3: List comprehension
list1 = [i for i in range(10)]
print(list1)		# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = [i for i in 'range(10)']
print(list2)		# ['r', 'a', 'n', 'g', 'e', '(', '1', '0', ')']

There are three commonly used methods to add list elements:

  • list.append('元素')Method: Add an element to the end of the list

  • list.insert(索引,'元素')Method: Insert an element at the specified position in the list

  • Iterative append, list.extend('字符串'): Add several elements to the last iteration of the list

li = [23, 4, 'ab', True]
li.extend('soft')               # 迭代追加是以最小元素为单位
print(li)
li.extend(['soft', 99, False])  # 最小单位是列表中的元素
print(li)
# 运行结果
[23, 4, 'ab', True, 's', 'o', 'f', 't']
[23, 4, 'ab', True, 's', 'o', 'f', 't', 'soft', 99, False]

Deletion of list elements

  • list.pop()方法: Delete the last element in the list by default. You can also delete the specified element according to the index position and return the deleted element.
li = [23, 4, 'ab', True]
print(li.pop(-2))			# 直接将返回值输出
>>>ab
  • list.remove()方法: Delete the specified element. If there is an element with the same name, delete the first one with the same name.
li = [23, 4, 'ab', True,'ab']
li.remove('ab')
print(li)
>>>[23, 4, True, 'ab']
  • list.clear()方法:clear the list

  • del 指令: Delete corresponding elements according to index (slicing)

li = [23, 4, 'ab', True,'ab',False,'amw',35,100,'kvi']
# 按照索引删除指定元素
del li[3]
print(li)
# 按照切片(步长)删除几个元素
del li[1::2]
print(li)
>>>[23, 4, 'ab', 'ab', False, 'amw', 35, 100, 'kvi']
   [23, 'ab', False, 35, 'kvi']

Modifications to list elements

  • Modify elements directly according to the index sequence number.
li = [23, 4, 'ab', True]
li[0]='tree'
print(li)
>>>['tree', 4, 'ab', True]
  • Modify several elements according to slice (step size)
li = [23, 4, 'ab', True,'ab',False,'amw',35,100,'kvi']
li[3:]='abcdefghijk'		# 从索引序号为3的位置开始对列表进行迭代修改
print(li)
>>>[23, 4, 'ab', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']

li = [23, 4, 'ab', True, 'ab', False, 'amw', 35, 100, 'kvi']
li[1::2] = ['1', 43, '*&%', 'qwr',888]		# 加了步长需应对修改元素个数
print(li)
>>>[23, '1', 'ab', 43, 'ab', '*&%', 'amw', 'qwr', 100, 888]

Query for list elements

  • You can use indexing, slicing (step size)
  • Use a for loop to traverse

2. Nesting of lists

for example

lis = [2, 30, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]
# 将列表lis中的"tt"变成大写(用两种方式)。
# lis[3][2][1][0] = 'TT'
lis[3][2][1][0] = lis[3][2][1][0].upper()

# 将列表中的数字3变成字符串"100"(用两种方式)。
# lis[3][2][1][1] = str(lis[3][2][1][1]+97)
lis[3][2][1][1] = '100'

# 将列表中的字符串"1"变成数字101(用两种方式)。
# lis[3][2][1][2] = int(lis[3][2][1][2])+100
lis[3][2][1][2] = 101

print(lis)
>>>[2, 30, 'k', ['qwe', 20, ['k1', ['TT', '100', 101]], 89], 'ab', 'adv']

3. Tuple

Tuple: It is a read-only list that stores a large amount of data and can be indexed, sliced ​​(step size), and traversed.

  • Format: tupe1 = (100,'python',True,[1,2,3])
tupe1 = (100, 'python', True, [1, 2, 3])
print(len(tupe1))       # 可以用len()方法获取元组里成员个数
print(tupe1[1])         # 直接按索引号打印元组里的数据
for t in tupe1:
    print(t)            # 用遍历的方法打印元组里的所有成员信息
# 元组里包含有列表的情况下,可以对列表里的成员进行修改
tupe1[3][0] = 666       
del tupe1[3][1]

Application of tuples

  • Tuples are used for unpacking and assigning values ​​separately.
a,b = 1,2
print(a,b)
>>>1 2
  • Tuples are used to store some fixed data that can be changed after definition: ID number, user name, password, etc.

Picture sharing

Insert image description here

Guess you like

Origin blog.csdn.net/Jo_Francis/article/details/125272247