Sequence object (sequence)
"Sequence" is a data storage method often used in programming. In other programming languages, a "sequence" is often called an "array", a data structure used to store related data items. Almost every programming language provides "sequence" data structures, such as one-dimensional and multi-dimensional arrays in C and Basic.
The difference between a sequence and an array:
- An array provides a continuous memory space that can store the same data type.
- Although columns are continuous storage spaces, they can store different data types and can also be understood as more "advanced arrays".
[Sequence Object]
Commonly used sequence objects in python
- List List (variable data type).
- Tuple Tuple (immutable data type).
- Collection Sets (variable data type).
- Dictionary Dictionary (variable data type).
- String String (immutable).
- range( ).
1. List List (type)
- List (list) is the most frequently used data type in Python.
- Lists can complete the data structure implementation of most collection classes. It supports characters, numbers, strings and can even contain lists (i.e. nested).
- Lists are marked with "[ ]" and are the most common composite data type in python.
How to create a list
Syntax: List object name = [Element 1, Element 2, ..., Element N].
Example: Two ways to create a list object.
Method 1: Default method
列表对象= [元素1,元素2,元素3, ...元素N, ]
>>> list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,]
>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list2 = [‘a’, ’b’, ‘c’, ‘d’, ‘e’, ‘f’, ]
>>> list2
[‘a’, ’b’, ‘c’, ‘d’, ‘e’, ‘f’]
>>> list3 = [‘a’, 1 , True , ‘Hello’, ]
>>> list3
[‘a’, 1 , True , ‘Hello’]
Example: Two ways to create a list object.
Method 2: Use range0 built-in function
- Python3 list) function is an object iterator, which can convert the iterable object returned by range() into a list, and the returned variable type for a list.
Syntax:
列表对象 = list(range(stop))
>>> list1 = list(range(10))
>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list2 = list(range(5, 10))
>>> list2
[5, 6, 7, 8, 9]
>>> list3 = list(range(0, 10, 2))
>>> list3
[0, 2, 4, 6, 8]
Python3 range() built-in function
- Python3 range0 function returns an iterable object, not a list type, so When printing, the list will not be printed.
Creation syntax
① range( stop )
② range(start, stop [, step])
Parameter description:
-
start: Counting starts from start. Default starts from 0. For example: range(5) is equivalent to range(0, 5).
-
stop: Counts up to and including stop. For example: range(0, 5) is [0, 1, 2, 3, 4] without 5.
-
step: step size, the default is 1. For example: range(0, 5) is equivalent to range(0, 5, 1).
How to access the list?
You can also use [head subscript: tail subscript: step] to cut the values in the list, and you can intercept the corresponding list.
- The subscript index from left to right starts with 0 by default, and the subscript index from right to left starts with -1 by default. The subscript can be empty to indicate that the head or tail is retrieved.
Code demonstration: ch03-demol-it-slicepy
##Run result:
Can the first element in the mList be modified?
List update
The so-called update refers to the reassignment, deletion, and addition of list elements and other related operations. -
Code demonstration: cho3-dem2-istupdate.py
##Running result:
List operators
We have already learned about the functions of and * in the introduction to strings. The usage of lists is similar to them, and there are also operations such as len() and in.
- Code demonstration: ch03-demo03-list-operation.py
Run result:
Functions in lists
In order to better operate list objects, Python also provides a lot of functions:
① len(list) : 获取列表元素的个数;
② max(list) : 获取列表中的最大值;
③ min(list) : 获取列表中的最小值;
④ list(seq) : 将元组对象转换成列表对象。
Lists also provide a lot of methods:
① .append(obj) :在列表末尾添加一个元素;
② .count(obj) : 统计某个元素在列表中出现的次数;
③ .index(obj) : 从列表中找出某个值第一个匹配的索引位置;
④ .insert(index, obj) : 向指定位置前序添加一个元素
⑤ .remove(obj) : 移除一个指定的元素;
⑥ .reverse() : 反向列表中的元素;
⑦ .sort() : 对列表进行排序;
The difference between List[] and list[:]
'[] "identification is a typical reference operation and address transfer operation.
- "[:]"identification, It is a typical assignment operation and value passing operation.
- Sample code:
List_1 = [ 1, 2, 3, 4]
List_2 = list_1
List_1 = [ 1, 2, 3, 4]
List_2 = list_1[:]
2、元组(Tuple)类型
- Tuple(元组)类似于List(列表)。
- 元组不能二次赋值(元组内的元素不允许更新),相当于只读列表。
- 元组用"() "标识。内部元素用逗号隔开。
如何创建元组?
语法:
元组对象名称 = ( 元素1, 元素 2, ……, 元素N )。
元组的特点
元组与列表的所有操作基本类似,唯一不一样的地方是,元组的元素不允许被修改。
示例代码:
>>> tup1 = tuple(range(5))
>>> tup1
(0, 1, 2, 3, 4)
>>> tup2 = (5, 6, 7,)
>>> tup2
(5, 6, 7)
特别说明:
>>> tup1 = s(1)
>>> type(tup1)
>>> tup1 =(1,)
>>> tup1
3、字典( Dictionary)类型
- 字典(dictionary)是除列表以外python之 中最灵活的内置数据结构类型。key:value
- 字典当中的元素是通过键来存取的 ,而不是通过偏移存取。
- 字典用"{}”标识。字典由索引(key)和它对应的值value组成,是一个典型的"k-v值”数据结构。
如何创建字典?
语法:
- 字典对象名称 = { }。
- 字典对象名称 = { key1 : value1, key2 : value2, …. , keyN : valueN}。
字典( Dictionary) k-v值在内存中的表现形式
Python 字典
- k-v结构- 般情况下在操作访问的时候都会使用key索弓进行每个元素的读取操作。
- 由于key索引键 会被频繁访问,因此索引键key存放在Stack栈内存中,而value值则存储在Heap堆内存中。
内存表现形式:
如何访问字典:
语法:
- 字典对象名称[ key ] #访问key对应的value值。
- 字典对象名称.keys #访问当前字典所有key索引键。
- 字典对象名称.values #访问当前字典所有valeus值。
代码演示: ch03-demo04.py
运行结果:
如何修改字典?
字典更新指的是对字段元素的重新赋值、删除、添加等相关操作。
相关方法:
- update( ) # 添加一个新元素或更新已有元素。
- pop( ) # 删除指定元素。
代码演示: cho3-demo05.py
运行结果 :
4、集合Set
- 集合是一个无序不重复元素的集。基本功能包括关系测试和消除重复元素。
- 可以用大括号({)创建集合。 注意:如果要创建一个空集合,你必须用set)而不是{} ;后者创建一个空的字典。
集合的创建
obj1 = {1, 2, 3, 4, 5}
obj2 = ({6, 7, 8, 9})
集合Set的关系操作(交.并、补)
s = set([3,5,9,10]) #创建- -个数值集合
t = set("Hello") #创建一个唯一 字符的集合
a=t|s # t和s的并集,
b=t&s # t和s的交集
C=t-S #求差集(项在t中,但不在s中)
d=t^s #对称差集(项在域s中,不会同时出现在二者中)
基本操作:
t.add('x") #添加一项
s.update([10,37,42]) #在s中添加多项
The above is the detailed content of Python masters and is familiar with list, ancestor, dictionary, and set data types. For more information, please follow other related articles on the PHP Chinese website!