QQ个性网:专注于分享免费的QQ个性内容

关于我们| 网站公告| 广告服务| 联系我们| 网站地图

搜索
编程 JavaScript Java C++ Python SQL C Io ML COBOL Racket APL OCaml ABC Sed Bash Visual Basic Modula-2 Logo Delphi IDL Groovy Julia REXX Chapel X10 Forth Eiffel C# Go Rust PHP Swift Kotlin R Dart Perl Ruby TypeScript MATLAB Shell Lua Scala Objective-C F# Haskell Elixir Lisp Prolog Ada Fortran Erlang Scheme Smalltalk ABAP D ActionScript Tcl AWK IDL J PostScript IDL PL/SQL PowerShell

Python元组的使用和方法

日期:2025/04/03 06:55来源:未知 人气:54

导读:一、创建元组元组(Tuples)与列表一样,属于Python中的序列类型,它是任意对象的有序集合,通过“位置”或者“索引”访问其中的元素,它具有可变长度、异构和任意嵌套的特点,与列表不同的是:元组中的元素是不可修改的。元组的创建很简单,把元素放入小括号,并在每两个元素中间使用逗号隔开即可,格式为:tuplename = (元素1, 元素2, 元素3, ……, 元素n)举例如下:......

一、创建元组

元组(Tuples)与列表一样,属于Python中的序列类型,它是任意对象的有序集合,通过“位置”或者“索引”访问其中的元素,它具有可变长度、异构和任意嵌套的特点,与列表不同的是:元组中的元素是不可修改的。

元组的创建很简单,把元素放入小括号,并在每两个元素中间使用逗号隔开即可,格式为:

tuplename = (元素1, 元素2, 元素3, ……, 元素n)

举例如下:

sample_tuple1 = (1, 2, 3, 4, 5, 6)

sample_tuple2 = "p", "y", "t", "h", "o", "n"

sample_tuple3 = ('python', 'sample', 'tuple', 'for', 'your', 'reference')

sample_tuple4 = ('python', 'sample', 'tuple', 1989, 1991, 2018)

元组也可以为空:

sample_tuple5 = ()

需要注意的是,为避免歧义,当元组中只有一个元素时,必须在该元素后加上逗号,否则括号会被当作运算符,例如:

sample_tuple6 = (123,)

元组也可以嵌套使用,例如:

sample_tuple1 = (1, 2, 3, 4, 5, 6)

sample_tuple2 = "P", "y", "t", "h", "o", "n"

sample_tuple7 = (sample_tuple1, sample_tuple2)

print (sample_tuple7)

((1, 2, 3, 4, 5, 6), ('P', 'y', 't', 'h', 'o', 'n'))

二、使用元组

与列表相同,我们可以通过使用“位置”或者“索引”来访问元组中的值,“位置”或者“索引”也是从0开始,例如:

sample_tuple1[1]表示元组tuple1中的第2个元素:2。

sample_tuple1[3:5] 表示元组sample_tuple1中的第4个和第5个元素,不包含第6个元素:4,5。

sample_tuple1[-2] 表示元组sample_tuple1中从右侧向左数的第2个元素:5。

代码示例为:

print (sample_tuple1[1]) #截取第2个元素

2

print (sample_tuple1[3:5]) #第4个和第5个元素,不包含第6个元素

(4, 5)

print (sample_tuple1[-2]) #从右侧向左数的第2个元素

5

元组也支持“切片”操作,例如

sample_tuple2 = "P", "y", "t", "h", "o", "n"

sample_tuple2[:] 表示取元组sample_tuple2的所有元素;

sample_tuple2[3:] 表示取元组sample_tuple2的索引为3的元素之后的所有元素;

sample_tuple2[0:4:2] 表示元组sample_tuple2的索引为0到4的元素,每隔一个元素取一个。

print (sample_tuple2[:]) #取元组sample_tuple2的所有元素

('P', 'y', 't', 'h', 'o', 'n')

print (sample_tuple2[3:]) #取元组的第4个元素之后的所有元素

('h', 'o', 'n')

print (sample_tuple2[0:4:2]) #元组sample_tuple2的第1个到第5元素,每隔一个元素取一个

('P', 't')

三、删除元组

由于元组中的元素是不可变的,也就是不允许被删除的,但可以使用del 语句删除整个元组:

del tuple

代码示例如下:

sample_tuple3 = ('Python', 'sample', 'tuple', 'for', 'your', 'reference')

print (sample_tuple3) #输出删除前的元组sample_tuple3

('Python', 'sample', 'tuple', 'for', 'your', 'reference')

del sample_tuple3 #删除元组sample_tuple3

print (sample_tuple3) #输出删除后的元组sample_tuple3

Traceback (most recent call last):

File "<pyshell#49>", line 1, in

print (sample_tuple3)

NameError: name ' sample_tuple3' is not defined #系统正常报告sample_tuple3没有定义

四、元组的内置函数

sample_tuple1 = (1, 2, 3, 4, 5, 6) #创建元组tuple1

print (len(sample_tuple1)) #输出元组长度

6

print (max(sample_tuple1)) #输出元组最大值

print (min(sample_tuple1)) #输出元组最小值

1

a = [1,2,3] #创建列表a

print (a) #输出列表a

[1, 2, 3]

print (tuple(a)) #转换列表a为元组后输出

(1, 2, 3)

关于我们|网站公告|广告服务|联系我们| 网站地图

Copyright © 2002-2023 某某QQ个性网 版权所有 | 备案号:粤ICP备xxxxxxxx号

声明: 本站非腾讯QQ官方网站 所有软件和文章来自互联网 如有异议 请与本站联系 本站为非赢利性网站 不接受任何赞助和广告