python- tuple data type

Test sites

如何定义一个空元组,请使用两种方式来定义
()
tuple()

定义一个元组,它只有一个数据,数据的值是3
(3,)

定义一个元组,它有多个数据,数据分别是 "hello"   "python"  "world"
("hello", "python", "world")

如何将列表转换为元组,请写出格式
得到的元组 = tuple(列表)

如何将元组转换为列表,请写出格式
得到的列表 = list(元组)

有一个元组stu_tuple,请获取索引值为2的数据
stu_tuple[2]

Appreciated tuples ordered container

And a list of similar tuples are ordered container

Sorted containers can be accommodated store data

Data in the container according to a position order is placed

If the count, from left to right, starting with 0

If the value, you can position the value of the index

Summed up the value of the container and orderly format

有序容器[索引值]

The difference with a list of tuples at

Features of the tuple, data security

For a list, a list of container, it can add data, you can delete data

Tuples can not

It would be a tuple it storing fixed data, the effect is fixed, the data will not be reduced, not increased, it will be unchanged

Tuple is a safe and orderly containers

for example:

python classes to classes, and a month later on classes, small partners Come and register it. . . .

Registration stage, the students can be registered

Registered data

name_list = []

At first no one enrollment, so register is empty

Then, slowly it was coming

Our name_list student's name recorded in the list will slowly become more

Some also is the case, registered students, the sudden cancellation of the registration

These are possible, you can add, you can delete, or modify

Once class begins, namely, classes, and began to class

Class rosters are no longer allowed to change the

After classes, the class will not receive new students to join or support the students leave

Equivalent to that class of persons, has been stabilized, and do not want change happen

Tuple data type, they are suitable for such a scenario

Definition of tuples

Tuple is defined in two ways

t = tuple()
print(type(t), t)


m = ()
print(type(m), m)

Since the additions and deletions to data tuples can not, therefore, empty tuple, there is not much practical significance

Definition of the content of the tuple

  • There are multiple definitions of content
元组 = (数据1,数据n)

例如:
t = (1,2,3)
  • Only the definition of a data tuple
元组 = (数据1,)

例子
t = (2,) 

Tuple values

变量 = 元组[索引值]

Here Insert Picture Description

Scenarios tuple

Tuple typically used to hold a series of data

user_info = ( "Lu small cloth", 18 "Wuhan", 1.83, [ "singing", "dance", "PK"])

List generally used for the same type of data stored

name_list = [ "Joe Smith", "John Doe", "Wang five"]

Conversion of a list of tuples

A list of turn-tuple

得到的元组 = tuple(列表)

例子
name_tuple = tuple(name_list)

Tuples transfer list

得到的列表 = list(元组)

例子
newdata = list(name_tuple)

Guess you like

Origin blog.csdn.net/ifubing/article/details/94344902