Sequence type operation 038 - tuples and lists Type

I. Summary

  • Sequence Type Definition
  • Method sequential processing function and
  • Tuple type and operating
  • List type and operation
  • Sequence type scenarios

Second, the sequence type is defined

Sequence is a set of elements has a relationship with a

  • Sequence is the one-dimensional vector elements, it may be different types of elements
  • Similar mathematical sequence of elements: s0, s1, … , sn-1
  • Guided by a number between elements, accessible by the subscript sequence specific element

Sequence is a base class

038- sequence type operation -? Tuples and lists types -01.jpg x-oss-process = style / watermark

2.1 Definition of numbers

038- sequence type operation -? Tuples and lists types -02.jpg x-oss-process = style / watermark

Third, the sequential processing function and method

Operators and Applications description
x in s If x is an element sequence s, returns True, otherwise False
x not in s If x is an element sequence s, returns False, otherwise True
s + t Connecting the two sequences s and t
S n-or n- S The replication sequence s n times
s[i] Index, s is returned in the i-th element, i is the sequence number
s [i: j] or s [i: j: k] Slice, returns i to j in steps of k in the sub-element of the sequence s

Examples of sequence type operation 3.1

ls = ['python', 123]
ls[::-1]
[123, 'python']
s = 'python123'
s[::-1]
'321nohtyp'

3.2 generic functions and methods type sequence

Functions and methods description
only (a) Returns the length of the sequence s
min (s) Returns the smallest element of the sequence s, s elements require comparable
max(s) Returns the maximum element of the sequence s, s elements require comparable
s.index(x) 或s.index(x, i, j) S return sequence from position i to j start position of the first occurrence of the element x
s.count(x) Returns the total number of x s appear in sequence
ls = ['python', 123]
len(ls)
2
s = 'python123'
max(s)
'y'

Fourth, the tuple type and operating

4.1 yuan group type definition

Tuple type is an extension sequence

  • Tuple is a sequence type, once created can not be modified
  • Use parentheses () or tuple () to create, between elements separated by commas,
  • You can use or not use parentheses
creature = "cat", "dog", "tiger", "human"
creature
('cat', 'dog', 'tiger', 'human')
color = (0x001100, "blue", creature)
color
(4352, 'blue', ('cat', 'dog', 'tiger', 'human'))

4.2 yuan operating group type

Tuple inherit all common types of operation sequence

  • Tuple inherits all generic sequence of operations of the type
  • Tuples can not be changed because the creation, so there is no special operation
  • With or without parentheses
creature = "cat", "dog", "tiger", "human"
creature[::-1]
('human', 'tiger', 'dog', 'cat')
color = (0x001100, "blue", creature)
color[-1][2]
'tiger'

Fifth, the list of types and operation

5.1 List type definition

The list is an extension of sequence type, very common

  • List is a sequence type, can be modified at will to create
  • 使用方括号 [] 或list() 创建,元素间用逗号 , 分隔
  • 列表中各元素类型可以不同,无长度限制
ls = ["cat", "dog", "tiger", 1024]
ls
函数或方法 描述
ls[i] = x 替换列表ls第i元素为x
ls[i: j: k] = lt 用列表lt替换ls切片后所对应元素子列表
del ls[i] 删除列表ls中第i元素
del ls[i: j: k] 删除列表ls中第i到第j以k为步长的元素
ls += lt 更新列表ls,将列表lt元素增加到列表ls中
ls *= n 更新列表ls,其元素重复n次
ls = ["cat", "dog", "tiger", 1024]
ls[1:2] = [1, 2, 3, 4]
ls
['cat', 1, 2, 3, 4, 'tiger', 1024]
del ls[::3]
ls
[1, 2, 4, 'tiger']
ls * 2
[1, 2, 4, 'tiger', 1, 2, 4, 'tiger']

5.2 列表类型操作函数和方法

函数或方法 描述
ls.append(x) 在列表ls最后增加一个元素x
ls.clear() 删除列表ls中所有元素
ls.copy() 生成一个新列表,赋值ls中所有元素
ls.insert(i,x) 在列表ls的第i位置增加元素x
ls.pop(i) 将列表ls中第i位置元素取出并删除该元素
ls.remove(x) 将列表ls中出现的第一个元素x删除
ls.reverse() 将列表ls中的元素反转
ls = ["cat", "dog", "tiger", 1024]
ls.append(1234)
ls
['cat', 'dog', 'tiger', 1024, 1234]
ls.insert(3, "human")
ls
['cat', 'dog', 'tiger', 'human', 1024, 1234]
ls.reverse()
ls
[1234, 1024, 'human', 'tiger', 'dog', 'cat']

5.3 列表功能默写

5.3.1 题目

  • 定义空列表lt
  • 向lt新增5个元素 --> lt += [1,2,3,4,5]
  • 修改lt中第2个元素
  • 向lt中第2个位置增加一个元素
  • 从lt中第1个位置删除一个元素
  • 删除lt中第1-3位置元素
  • 判断lt中是否包含数字0
  • 向lt新增数字0
  • 返回数字0所在lt中的索引
  • lt的长度
  • lt中最大元素
  • 清空lt

5.3.2 答案

  • lt = []
  • lt += [1,2,3,4,5]
  • lt[2] = 6
  • lt.insert(2, 7)
  • del lt[1]
  • del lt[1:4]
  • 0 in lt
  • lt.append(0)
  • lt.index(0)
  • len(lt)
  • max(lt)
  • lt.clear()

六、序列类型应用场景

数据表示:元组 和 列表

  • 元组用于元素不改变的应用场景,更多用于固定搭配场景
  • 列表更加灵活,它是最常用的序列类型
  • 最主要作用:表示一组有序数据,进而操作它们

6.1 元素遍历

for item in ls/tp :
    <语句块>

6.2 数据保护

  • 如果不希望数据被程序所改变,转换成元组类型
ls = ["cat", "dog", "tiger", 1024]
lt = tuple(ls)
lt
('cat', 'dog', 'tiger', 1024)

七、单元小结

  • 序列是基类类型,扩展类型包括:字符串、元组和列表
  • Tuple with () and tuple () to create, create a list with [] and set ()
  • Tuple operation sequence substantially identical to the operation
  • List of operations in sequence on the basis of the operation, an increase of more flexibility

Guess you like

Origin www.cnblogs.com/nickchen121/p/11200518.html