Python干货:了解元组与列表的使用和区别

2021-01-26  本文已影响0人  七喜c4d

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

 列表与元组

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

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

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

推荐一个学习交流的q裙,大家在学习Python的过程中遇到了什么问题都可以发群里,大家一起讨论交流,一起学习一起进步。610 380 249

列表

1.列表中的append()和extend() 都是对列表增加元素的方法,都不支持多参数 但是append()向列表中添加一个作为整体的对象, extend()是把一个可迭代对象的内容迭代添加到列表中

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

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

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

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

元组

存储在元组中的值序列可以是任何类型的,并且它们由整数编制索引。 元组的值在语法上用"逗号"分隔。 但通过关闭括号中的值序列来定义元组更为常见。

创建一个空元组与创建带一个元素的元组

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

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

tup1 = () # 创建空元组

tup2 = (1, )

#元组中只包含一个元素时,需要在元素后面添加逗号

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')

Python的元组与列表类似,不同之处在于元组的元素不能修改。

删除元组

元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下实例:

tup = ('physics', 'chemistry', 1997, 2000)

print(tup)

del tup print("After deleting tup:")

print(tup)

元组使用小括号,列表使用方括号。

元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。

tup1 = ('physics', 'chemistry', 1997, 2000)tup2 = (1, 2, 3, 4, 5 )tup3 = "a", "b", "c", "d"

元组与字符串类似,下标索引从0开始,可以进行截取,组合等。

修改元组

元组中的元素值是不允许修改的,但我们可以对元组进行连接组合,如下实例:

tup1 = (1,2,3,4)

tup2 = ('abc', 'xyz')

# 以下修改元组元素操作是非法的。

# tup1[0] = 100

# 创建一个新的元组

tup3 = tup1 + tup2

print (tup3)

元组运算符

与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。

元组索引,截取(切片)

因为元组也是一个序列,所以我们可以访问元组中的指定位置的元素,也可以截取索引中的一段元素,如下所示:

L =('spam','Spam','SPAM!')

无关闭分隔符

任意无符号的对象,以逗号隔开,默认为元组,如下实例:

print ('abc', -4.24e93, 18+6.6j, 'xyz')

x, y = 1, 2

print ("Value of x , y : ", x,y)

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

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

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

#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')

图们的切片

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

也可以对列表和数组进行切片。在列表中索引结果获取单个元素,而且切片允许获取一组元素。 注意- 负增量值也可用于反转元数序列

# 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)

内置方法

上一篇 下一篇

猜你喜欢

热点阅读