Python干货整理:一分钟了解元组与列表使用与区别

142 阅读4分钟

元组是 Python 对象的集合,跟列表十分相似。下面进行简单的对比。

列表与元组

1、python中的列表list是变量,而元组tuple是常量。

列表:是使用方括号[],元组:则是使用圆括号()

2、两者都可以使用索引读取值

列表

1.列表中的append()和extend()

都是对列表增加元素的方法,都不支持多参数

但是append()向列表中添加一个作为整体的对象,

extend()是把一个可迭代对象的内容迭代添加到列表中

2. 列表中的remove()、pop()和del

remove:删除单个元素,删除首个符合条件的元素,按值删除,返回值为空

pop:删除索引位置元素,无参情况下删除最后一个元素,返回删除的元素值

del:简单粗暴,可传索引值参数删除符合条件的元素,也可不接参数整个删除

元组

存储在元组中的值序列可以是任何类型的,并且它们由整数编制索引。

元组的值在语法上用"逗号"分隔。

但通过关闭括号中的值序列来定义元组更为常见。

创建元组

在 Python 中,通过放置用"逗号"分隔的值序列(带或不使用括号来分组数据序列)来创建元组。

注 :创建不使用括号的 Python 元组称为元组打包

Python 程序演示在元组中添加的元素

# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))

#Creating a Tuple 
#with the use of built-in function
Tuple1 = tuple('geeen')
print("\nTuple with the use of function: ")
print(Tuple1)

输出:

Initial empty Tuple: 
()

Tuple with the use of String: 
('Geeks', 'For')

Tuple using List: 
(1, 2, 4, 5, 6)

Tuple with the use of function: 
('G', 'e', 'e', 'e', 'n')

创建具有混合数据类型的元组

元组可以包含任何数量的元素和任何数据类型(如字符串、整数、列表等)。

也可以使用单个元素创建元组。

括号中包含一个元素是不够的,必须有一个尾随的"逗号"才能使其成为元组。

#Creating a Tuple
#with nested tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('python', 'geeen')
Tuple3 = (Tuple1, Tuple2)
print("\nTuple with nested tuples: ")
print(Tuple3)

#Creating a Tuple
#with repetition
Tuple1 = ('Geeen',) * 3
print("\nTuple with repetition: ")
print(Tuple1)

#Creating a Tuple 
#with the use of loop
Tuple1 = ('Geeen')
n = 5
print("\nTuple with a loop")
for i in range(int(n)):
    Tuple1 = (Tuple1,)
    print(Tuple1)

输出:

Tuple with Mixed Datatypes: 
(5, 'Welcome', 7, 'Geeen')

Tuple with nested tuples: 
((0, 1, 2, 3), ('python', 'geeen'))

Tuple with repetition: 
('Geeen', 'Geeen', 'Geeen')

Tuple with a loop
('Geeen',)
(('Geeen',),)
((('Geeen',),),)
(((('Geeen',),),),)
((((('Geeen',),),),),)

访问元组

元组是不可变的,通常,它们包含一系列异构元素。

这些元素是通过解包或索引(甚至按属性在命名元组的情况下访问)。

列表是可变的,并且其元素通常是同质的,并且通过遍该列表进行遍时访问。

注意:左侧元组数的变量时,应等于给定元组 a 中的值数。

#Accessing Tuple
#with Indexing
Tuple1 = tuple("Geeen")
print("\nFirst element of Tuple: ")
print(Tuple1[1])

#Tuple unpacking
Tuple1 = ("Geeen", "For", "Geeen")

#This line unpack 
#values of Tuple1
a, b, c = Tuple1
print("\nValues after unpacking: ")
print(a)
print(b)
print(c)

输出:

First element of Tuple:
e

Values after unpacking:
Geeen
For
Geeen

图组串联

元组串联是两个或更多元组连接的过程。其他算术运算不适用于元对元。

串联通过使用"+"运算符完成。元组串联始终从原始元组末尾完成。

**注意 -**只有相同的数据类型可以与串联结合,如果将列表和元组组合在一起,则会出现错误。

# Concatenaton of tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('Geeen', 'For', 'Geeen')

Tuple3 = Tuple1 + Tuple2

# Printing first Tuple
print("Tuple 1: ")
print(Tuple1)

# Printing Second Tuple
print("\nTuple2: ")
print(Tuple2)

# Printing Final Tuple
print("\nTuples after Concatenaton: ")
print(Tuple3)

输出:

Tuple 1: 
(0, 1, 2, 3)

Tuple2: 
('Geeen', 'For', 'Geeen')

Tuples after Concatenaton: 
(0, 1, 2, 3, 'Geeen', 'For', 'Geeen')

图们的切片

执行元组切片以从元组获取特定范围或子元素切片。

也可以对列表和数组进行切片。在列表中索引结果获取单个元素,而且切片允许获取一组元素。

注意- 负增量值也可用于反转元数序列

![](https://p3-tt-ipv6.byteimg.com/origin/pgc-image/f3218937f102490ebcce65e7a55392ba)
# Slicing of a Tuple
# wit

输出:

Removal of First Element: 
('E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S')

Tuple after sequence of Element is reversed: 
('S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G')

Printing elements between Range 4-9: 
('S', 'F', 'O', 'R', 'G')

删除元组

元组是不可变的,因此它们不允许删除其中的一部分。使用 del() 方法将删除整个元组。

**注意 -**删除后打印元组结果为错误。

# Deleting a Tuple

Tuple1 = (0, 1, 2, 3, 4)
del Tuple1

print(Tuple1)

内置方法

![](https://p26-tt.byteimg.com/origin/pgc-image/5ddc92eafe154335a83e385562f59e50)