元组

作者: 庵下桃花仙 | 来源:发表于2018-12-07 22:28 被阅读15次

逗号隔开

In [3]: tup = 4, 5, 6

In [4]: tup
Out[4]: (4, 5, 6)

In [5]: nested_tup = (4, 5, 6), (7, 8)

In [6]: nested_tup
Out[6]: ((4, 5, 6), (7, 8))

tuple函数将任意序列或迭代前转换为元组

In [7]: tuple([4, 0, 2])
Out[7]: (4, 0, 2)

In [8]: tup = tuplr('string')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-64894eb043d3> in <module>
----> 1 tup = tuplr('string')

NameError: name 'tuplr' is not defined

In [9]: tup = tuple('string')

In [10]: tup
Out[10]: ('s', 't', 'r', 'i', 'n', 'g')

In [11]: tup[0]
Out[11]: 's'
In [12]: tup = tuple('foo', [1,2], True)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-ec2d6fb5a293> in <module>
----> 1 tup = tuple('foo', [1,2], True)

TypeError: tuple expected at most 1 arguments, got 3

In [13]: tup = tuple(['foo', [1, 2], True])

In [14]: tup
Out[14]: ('foo', [1, 2], True)

In [15]: tup[2] = False
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-b89d0c4ae599> in <module>
----> 1 tup[2] = False

TypeError: 'tuple' object does not support item assignment

In [16]: tup[1].append(3)

In [17]: tup
Out[17]: ('foo', [1, 2, 3], True)
In [18]: (4, None, 'foo') + (6, 0) + ('bar',)
Out[18]: (4, None, 'foo', 6, 0, 'bar')

In [19]: ('foo', 'bar') * 4
Out[19]: ('foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar')

元组拆包

如果想将元组型表达式赋值给变量,Python会对等号右边的值进行拆包。

In [1]: tup = (4, 5, 6)

In [2]: a, b, c = tup

In [3]: b
Out[3]: 5

In [4]: tup = 4, 5, (6, 7)

In [5]: a, b, (c, d) = tup

In [6]: d
Out[6]: 7

In [7]: a, b = 1, 2

In [8]: a
Out[8]: 1

In [9]: b
Out[9]: 2

In [10]: b, a = a, b

In [11]: a
Out[11]: 2

In [12]: b
Out[12]: 1

拆包常用的场景:

1、遍历元组或列表组成的序列;
2、从函数返回多个值

In [13]: seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

In [14]: for a, b, c in seq:
    ...:     print('a = {0}, b = {1}, c = {2}'.format(a, b,c))
a = 1, b = 2, c = 3
a = 4, b = 5, c = 6
a = 7, b = 8, c = 9

从元组的起始位置采集一些元素。特殊语法*rest,用于在函数调用时获取任意长度的参数列表。

rest有时是想要丢掉的数据,rest这个变量名没有特别之处,有时用下划线表示不想要的量。

In [15]: values = 1, 2, 3, 4, 5

In [16]: a, b, *rest = values

In [17]: a, b
Out[17]: (1, 2)

In [18]: rest
Out[18]: [3, 4, 5]

In [19]: a, b, *_ = values

In [20]: a
Out[20]: 1

In [21]: b
Out[21]: 2

In [22]: a,b
Out[22]: (1, 2)

In [23]: _
Out[23]: [3, 4, 5]

元组方法

In [24]: a = (1, 2, 2, 2, 3, 4, 2)

In [25]: a.count(2)
Out[25]: 4

相关文章

  • Python入门:元组

    六、元组 6.1 定义元组 元组和列表相似,列表是[---],元组是(---) 6.2 访问元组 6.3 修改元组...

  • Python 元组

    元组的创建和删除 访问元组元素 修改元组元素 元组推导式 元组与列表的区别

  • python入坑第七天|元组

    废话不多说,今天来学习元组。内容如下: 元组的创建 索引、切片 元组的连接 元组的不可修改性 元组内置函数 元组的...

  • Python元组

    python元组元组和列表的区别在于元组中的元素不能修改 创建元组创建元组用() tuple = ()当元组里只包...

  • Python_4_内置结构-元组-字符串

    1. 元组概念1.1. 元组的特点1.2. 元组的定义1.3. 元组的访问1.4. 元组的查询 2. 命名元组 3...

  • Swift 元组 (Tuple)

    定义元组 获取元组内容 修改元组 元组分解 元组作为函数返回值 通常可以用元组来为函数返回多个返回值。

  • 13、Python集合(set)

    上集回顾: 元组(tuple)定义 元组注意事项 元组妙用 上集学习了元组相关知识,元组和列表类似,但是不能修改。...

  • 3.元组Tuple

    目录0.元组介绍1.元组定义和初始化2.元组元素访问3.命名元组namedtuple 0.元组介绍 元组是不可变对...

  • swift 元组 (Tuple)

    元组的声明 输出结果 元组解包 输出结果 元组的分量 输出结果 命名元组分量 输出结果 使用_忽略元组分量

  • 元组

    目录 元组基本介绍 可变对象 元组和列表的区别 元组的解包(Unpacking) 1. 元组基本介绍 元组表现形式...

网友评论

    本文标题:元组

    本文链接:https://www.haomeiwen.com/subject/uixfhqtx.html