tuple(元组)

tuple (元组)

  • 可以理解成一个不允许更改的列表
help(tuple)
Help on class tuple in module builtins:

class tuple(object)
 |  tuple(iterable=(), /)
 |  
 |  Built-in immutable sequence.
 |  
 |  If no argument is given, the constructor returns an empty tuple.
 |  If iterable is specified the tuple is initialized from iterable's items.
 |  
 |  If the argument is a tuple, the return value is the same object.
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(self, key, /)
 |      Return self[key].
 |  
 |  __getnewargs__(self, /)
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __rmul__(self, value, /)
 |      Return value*self.
 |  
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |  
 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.
 |      
 |      Raises ValueError if the value is not present.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.

# tuple的创建

# 1.直接用小括号
ta = ()
print(type(ta))

# 当用小括号创建一个元素的tuple的时候
tb = (100)
print(type(tb))
tc = (100,)
print(type(tc))
td = (100,200,300)
print(type(td))
print(td)

# 2.直接用逗号
ta = 100
print(type(ta))
tb = 100,200,300, # 后面可以跟逗号
print(type(tb))

# 3. 使用tuple定义
ta =tuple()
print(type(ta))

li = [1,2,3,"wang caipo"]
tb = tuple(li) # 要求tuple参数必须可迭代
print(tb)
print(li)
<class 'tuple'>
<class 'int'>
<class 'tuple'>
<class 'tuple'>
(100, 200, 300)
<class 'int'>
<class 'tuple'>
<class 'tuple'>
(1, 2, 3, 'wang caipo')
[1, 2, 3, 'wang caipo']

tuple其余特征跟list基本一致

  • 有序
  • 可以访问不可用被修改
  • 元素可以是任意类型
# tuple索引操作
la = ["asd","qwe","wqere"]
print(la)
ta = tuple(la)
print(ta)
print(ta[2])
['asd', 'qwe', 'wqere']
('asd', 'qwe', 'wqere')
wqere
# tuple分片操作
print(ta[:])
print(ta[:2])
print(ta[-1::-1])
('asd', 'qwe', 'wqere')
('asd', 'qwe')
('wqere', 'qwe', 'asd')
# 元组的相加
ta = 100,200,300
tb = ("i","love","lq")
tc = ta + tb
print(tc)

(100, 200, 300, 'i', 'love', 'lq')

# tuple乘法

tc = tb * 2
print(tc)

('i', 'love', 'lq', 'i', 'love', 'lq')

# tuple 成员检测
print(tb)

if "lq" in tb:
    print("对的")
    
if "love" not in tb:
    print("不爱了,不爱了")

('i', 'love', 'lq')
对的

# 元组遍历

for i in tb:
    print(i)

i
love
lq

ta = ((10,20,30),("i","love","lq"),(100,200,300))
# 嵌套元组的访问
# 1.双层循环访问
for i in ta:
    print(i)
    for j in i:
        print(j)
        
# 2.使用单层循环
for i,j,k in ta:
    print(i,j,k)
    
# 上面访问有一个规定,即i,j,k要跟元组的元素个数对应
for i,j,k,q in ta:
    print(i,j,k,q)

(10, 20, 30)
10
20
30
('i', 'love', 'lq')
i
love
lq
(100, 200, 300)
100
200
300
10 20 30
i love lq
100 200 300

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-22-1b275115bd17> in <module>
     12 
     13 # 上面访问有一个规定,即i,j,k要跟元组的元素个数对应
---> 14 for i,j,k,q in ta:
     15     print(i,j,k,q)

ValueError: not enough values to unpack (expected 4, got 3)

# 常用元组函数
ta = (1,2,3,4,5,6,7,8,9)
# len:取长度
print(len(ta))
# max/min:最大最小值
print(max(ta))
print(min(ta))

tb = (1,2,3,"love")
print(max(tb))

9
9
1

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-26-5a521e37c458> in <module>
      8 
      9 tb = (1,2,3,"love")
---> 10 print(max(tb))

TypeError: '>' not supported between instances of 'str' and 'int'

# count:对某一元素计数
ta = (1,2,3,4,23,1,1,1,1,1,1,11)
print(ta.count(1))
# index:某一元素所在位置
print(ta.index(1))

7
0

# tuple的特殊用法
a = 100
b = "wang caipo"

# 要求对a,b值进行互换
# 此种用法是python的独门秘籍
print(a,b)
a, b = b, a
print(a,b)

100 wang caipo
wang caipo 100

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值