Python数据类型详解(三)元祖:tuple

yipeiwu_com5年前Python基础

一.基本数据类型

  整数:int
  字符串:str(注:\t等于一个tab键)
  布尔值: bool
  列表:list
  列表用[]
  元祖:tuple
  元祖用()

  字典:dict
注:所有的数据类型都存在想对应的类列里,元祖和列表功能一样,列表可以修改,元祖不能修改。

二.列表所有数据类型:

基本操作:

索引,切片,长度,包含,循环

class tuple(object):
  """
  tuple() -> empty tuple
  tuple(iterable) -> tuple initialized from iterable's items
  
  If the argument is a tuple, the return value is the same object.
  """
  def count(self, value): # real signature unknown; restored from __doc__
    """ T.count(value) -> integer -- return number of occurrences of value """
    (T.count(价值)- >整数,返回值的出现次数)
    return 0

  def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
    """
    T.index(value, [start, [stop]]) -> integer -- return first index of value.
    Raises ValueError if the value is not present.
    """
    (T。指数(价值,[开始,[不要]])- >整数,返回第一索引值。提出了ValueError如果不存在的价值。)
    return 0

  def __add__(self, *args, **kwargs): # real signature unknown
    """ Return self+value. """
    pass

  def __contains__(self, *args, **kwargs): # real signature unknown
    """ Return key in self. """
    pass

  def __eq__(self, *args, **kwargs): # real signature unknown
    """ Return self==value. """
    pass

  def __getattribute__(self, *args, **kwargs): # real signature unknown
    """ Return getattr(self, name). """
    pass

  def __getitem__(self, *args, **kwargs): # real signature unknown
    """ Return self[key]. """
    pass

  def __getnewargs__(self, *args, **kwargs): # real signature unknown
    pass

  def __ge__(self, *args, **kwargs): # real signature unknown
    """ Return self>=value. """
    pass

  def __gt__(self, *args, **kwargs): # real signature unknown
    """ Return self>value. """
    pass

  def __hash__(self, *args, **kwargs): # real signature unknown
    """ Return hash(self). """
    pass

  def __init__(self, seq=()): # known special case of tuple.__init__
    """
    tuple() -> empty tuple
    tuple(iterable) -> tuple initialized from iterable's items
    
    If the argument is a tuple, the return value is the same object.
    # (copied from class doc)
    """
    pass

  def __iter__(self, *args, **kwargs): # real signature unknown
    """ Implement iter(self). """
    pass

  def __len__(self, *args, **kwargs): # real signature unknown
    """ Return len(self). """
    pass

  def __le__(self, *args, **kwargs): # real signature unknown
    """ Return self<=value. """
    pass

  def __lt__(self, *args, **kwargs): # real signature unknown
    """ Return self<value. """
    pass

  def __mul__(self, *args, **kwargs): # real signature unknown
    """ Return self*value.n """
    pass

  @staticmethod # known case of __new__
  def __new__(*args, **kwargs): # real signature unknown
    """ Create and return a new object. See help(type) for accurate signature. """
    pass

  def __ne__(self, *args, **kwargs): # real signature unknown
    """ Return self!=value. """
    pass

  def __repr__(self, *args, **kwargs): # real signature unknown
    """ Return repr(self). """
    pass

  def __rmul__(self, *args, **kwargs): # real signature unknown
    """ Return self*value. """
    pass

三.所有元祖数据类型举例

#count 用于计算元素出现的个数
name_tuple = ("zhangyanlin","suoning","nick")
print(name_tuple.count('zhangyanlin'))
 
#index获取指定元素的指定位置
name_tuple = ("zhangyanlin","suoning","nick")
print(name_tuple.index('zhangyanlin'))

四.索引

name_tuple = ("zhangyanlin","suoning","nick")
print(name_tuple[1])

五.切片

#取出第一位到最后一位减1的元素
name_tuple = ("zhangyanlin","suoning","nick")
print(name_tuple[0:len(name_tuple)-1])

六.总长度len

#取出最后一位减1的元素
name_tuple = ("zhangyanlin","suoning","nick")
print(name_tuple[len(name_tuple)-1])

七.for循环

name_tuple = ("zhangyanlin","suoning","nick")
for i in name_tuple:
  print(i)

那么使用 tuple 有什么好处呢?

Tuple 比 list 操作速度快。如果您定义了一个值的常量集,并且唯一要用它做的是不断地遍历它,请使用 tuple 代替 list。
如果对不需要修改的数据进行 “写保护”,可以使代码更安全。使用 tuple 而不是 list 如同拥有一个隐含的 assert 语句,说明这一数据是常量。如果必须要改变这些值,则需要执行 tuple 到 list 的转换 (需要使用一个特殊的函数)。

还记得我说过 dictionary keys 可以是字符串,整数和 “其它几种类型”吗?Tuples 就是这些类型之一。Tuples 可以在 dictionary 中被用做 key,但是 list 不行。实际上,事情要比这更复杂。Dictionary key 必须是不可变的。Tuple 本身是不可改变的,但是如果您有一个 list 的 tuple,那就认为是可变的了,用做 dictionary key 就是不安全的。只有字符串、整数或其它对 dictionary 安全的 tuple 才可以用作 dictionary key。

Tuples 可以用在字符串格式化中,我们会很快看到。

相关文章

Python复制文件操作实例详解

本文实例讲述了Python复制文件操作用法。分享给大家供大家参考,具体如下: 这里用python实现了一个小型的自动发版本的工具。这个“自动发版本”有点虚, 只是简单地把debug 目录...

浅谈pandas中shift和diff函数关系

通过?pandas.DataFrame.shift命令查看帮助文档 Signature: pandas.DataFrame.shift(self, periods=1, fr...

解决Python print 输出文本显示 gbk 编码错误问题

前阵子想爬点东西,结果一直提示错误UnicodeEncodeError: ‘gbk' codec can't encode character ‘\xa0' in position,在网...

Python3安装psycopy2以及遇到问题解决方法

Python3安装psycopy2以及遇到问题解决方法

事先在网上搜索了一大圈,头都大了,看到那么多文章写道在python里安装psycopg2的各种坑和各种麻烦,各种不成功。搜索了一下午,索性外出放松。晚饭后,又继续上psycopg2官网(...

pytorch中的transforms模块实例详解

pytorch中的transforms模块中包含了很多种对图像数据进行变换的函数,这些都是在我们进行图像数据读入步骤中必不可少的,下面我们讲解几种最常用的函数,详细的内容还请参考pyto...