栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 元组

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 元组

python-元组
  • 元组的定义
    • 元组的创建
      • 使用tuple()创建
      • 直接赋值创建
      • 生成器创建元组
    • 元组的切片
    • 元组的方法
      • count()方法
      • index()方法
      • 内置方法sum(),max(),min(),sorted()
      • reversed()生成的是对象 - 翻转元组
    • zip()方法

元组的定义

元组与字符串一样属于不可变序列,这和列表不一样,同时意味者元组与列表相比,失去了增,删,改元素的方法。

元组的创建

创建元组和创建列表类似,可以使用tuple创建,也可以使用直接赋值的方法创建,需要注意的一点就是元组是"()",

使用tuple()创建

tuple()把可迭代对象转换成元组。

a = [1, 2, 3, 4, 5, 6]
b = 'i love python'
tuplea = tuple(a)
tuplea
(1, 2, 3, 4, 5, 6)
tupleb = tuple(b)
tupleb
('i', ' ', 'l', 'o', 'v', 'e', ' ', 'p', 'y', 't', 'h', 'o', 'n')
直接赋值创建

直接赋值 创建需要注意的是==“,”,小括号可有可无==。

tupled = (2,'ok',1.222,False)		#带小括号
tupled
(2, 'ok', 1.222, False)
tupled = 3,'failed',2.111,True		#不带小括号
tupled
(3, 'failed', 2.111, True)
tuplede = (1)				#只有一个元素时,创建元组需要留意小括号的使用。
tuplede
1
type(tuplede)
			#只有一个元素,尾部没有添加",",变量类型为整数。
tupledf = 1,			#只有一个元素,尾部添加了",",没有使用小括号创建元组。
type(tupledf)
			#类型为tule。
tupledf
(1,)
生成器创建元组

生成器创建与推导式没有形式上的不同,本质上不同的一点是使用生成器生成的是可迭代对象,需要对生成的对象进行处理[可转列表,元组。]遍历其中数据,也可以使用其自身的__next__()方法步进行式的遍历。
迭代对象只可遍历一次,第二次或以上会返回空,使用__next__()方法步进遍历最后一个数据时,再调用__next__()方法,会报错。
列表推导式使用"[]“生成的是列表,而生成器使用”()"。

itera = (i**3 for i in range(1,18,3))
itera			#生成的是generator对象。
 at 0x000001A53C2EF530>
itera.__next__()		#可使用__next__()方法进行步进式调用。
1
itera.__next__()
64
tupleh = tuple(itera)   #也可以使用tuple或list遍历式调用,由于前面二步合用
tupleh						#__next__()方法,此时gener对象的指针移到引3个位置。
(343, 1000, 2197, 4096)		#使用的是元组,生成的是元组。
iterb = (j+100 for j in range(4))
list(iterb)			#使用列表遍历,生成的是列表。
[100, 101, 102, 103]
元组的切片

这个和字符串,列表的切片没啥不同。再此不赘述。

元组的方法 count()方法

计算元素name在元组中出现的次数;

tuplea = (1, 2, 3, 1, 4, 5, 1, 6, 7, 1, 8, 9, 2, 2, 3)
tuplea.count(1)
4
tuplea.count(2)
3
tuplea.count(4)
1
index()方法

计算元素name在元组中出现的索引位置,以0开始。使用方法
tuple.index(name,start=0,end=~),可以指定开始与结束位置。想完全遍历name元素的所有索引位置可以用循环。

tuplea = (1, 2, 3, 1, 4, 5, 1, 6, 7, 1, 8, 9, 2, 2, 3)
tuplea.index(1)
0
内置方法sum(),max(),min(),sorted()

调用sorted内置方法,将会生成新的对象,这与列表自有方法list.sort()不同,需要留 意。

tuplea = (1, 2, 3, 1, 4, 5, 1, 6, 7, 1, 8, 9, 2, 2, 3)
sorted(tuplea)  #从小到大排序,生成了一个新的对象。
[1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9]
tuplea     #tuplea没有改变。
(1, 2, 3, 1, 4, 5, 1, 6, 7, 1, 8, 9, 2, 2, 3)
sorted(tuplea,reverse = True)  #从小到大排序并翻转元组。
[9, 8, 7, 6, 5, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1]
reversed()生成的是对象 - 翻转元组
tupled = (3, 'failed', 2.111, True)
tuple(reversed(tupled))
(True, 2.111, 'failed', 3)
tuplea = (1, 2, 3, 1, 4, 5, 1, 6, 7, 1, 8, 9, 2, 2, 3)
tuple(reversed(tuplea))
(3, 2, 2, 9, 8, 1, 7, 6, 1, 5, 4, 1, 3, 2, 1)
zip()方法
tupla =1,2,3
tuplb = 4,5,6
tuplc = 7,8,9
zip(tupla,tuplb,tuplc)

zipd = zip(tupla,tuplb,tuplc)
tupled = tuple(zipd)
tupled
((1, 4, 7), (2, 5, 8), (3, 6, 9))
tuplb = 'abc'
zipd = zip(tupla,tuplb,tuplc)
tupled = tuple(zipd)
tupled
((1, 'a', 7), (2, 'b', 8), (3, 'c', 9))
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1012227.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号