zoukankan      html  css  js  c++  java
  • Python内置函数(64)——tuple

    英文文档:

      The constructor builds a tuple whose items are the same and in the same order as iterable‘s items. iterable may be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a tuple, it is returned unchanged. For example, tuple('abc') returns ('a', 'b', 'c') and tuple( [1, 2, 3] ) returns (1, 2, 3). If no argument is given, the constructor creates a new empty tuple, ().

    说明:

      1. 函数功能创建一个新的元组。

      2. 不传入任何参数函数将创建一个空的元组。

    #不传入参数,创建空元组
    >>> tuple() 
    ()

      3. 函数可以接收1个可迭代对象作为参数,将使用可迭代对象的每个元素创建一个新的元组。

    #传入不可迭代对象,不能创建新的元组
    >>> tuple(121)
    Traceback (most recent call last):
      File "<pyshell#17>", line 1, in <module>
        tuple(121)
    TypeError: 'int' object is not iterable
    
    #传入可迭代对象。使用其元素创建新的元组
    >>> tuple('121')
    ('1', '2', '1')
    >>> tuple([1,2,1])
    (1, 2, 1)
    >>> tuple((1,2,1))
    (1, 2, 1)

      4. 创建新的元组还可以使用一对括号的方式:

      4.1 使用一对括号来创建空的元组。

    >>> a= ()
    >>> a
    ()

      4.2 创建单个元素的元组时必须尾随逗号。

    >>> a = (1,)
    >>> a #a是元组
    (1,)
    >>> a = (1)
    >>> a #a是数值
    1

      4.3 创建多个元素的元组,依次用逗号隔开。  

    >>> a = (1,2,3)
    >>> a
    (1, 2, 3)
  • 相关阅读:
    Kubernetes 部署Mysql 主从复制集群
    Kubernetes Kube-proxy
    vRO Extend VirtualDisk Workflow
    Kubernetes Horizontal Pod Autoscaler
    vRO 7 添加RestHost证书报错
    Kubernetes Resource Qoutas
    Kubernetes 命令补全
    Kubernetes 部署Weave Scope监控
    Kubernetes Yaml
    Go reflect反射
  • 原文地址:https://www.cnblogs.com/sesshoumaru/p/6129153.html
Copyright © 2011-2022 走看看