内置数据类型(tuple)

3.Tuple 介绍 

      Tuple 是不可变的 list。一建了一个 tuple,不能以任何方改变它。 

         Tuple 没有方 法 

>>> t
('a', 'b', 'mpilgrim', 'z', 'example')
>>> t.append("new") (1)
Traceback (innermost last):
File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'append'
>>> t.remove("z") (2)
Traceback (innermost last):
File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'remove'
>>> t.index("example") (3)
Traceback (innermost last):
File "<interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'index'
>>> "z" in t (4)
True

那么使用 tuple 有

  • Tuple list 。如果您定了一个常量集,用它的是不历它,请使用 tuple 代list。 

  • Tuples 可以在 dictionary 中key,但
    是 list 不行。
    上,事情要这更复。Dictionary key 必须是不可变的。Tuple 本是不可改变的,但是如果您有一个 list 的 tuple,那就认为是可变的了,用dictionary key 是不安的。字符串数或它对 dictionary 安的 tuple 可以用作 dictionary key 

  1. Note: Tuple 到 list 到 tuple 

  1. Tuple 可以转换成 list,然 

  1. 果上看,tuple 结一个 list,list 一个 tuple。 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值