Python学习笔记(二):tuple属性及方法浅析
【摘要】 元组是存放任意元素集合,不能修改其内容.
简单创建元组
In [1]: T = () #创建一个空的元组 In [2]: TOut[2]: () In [3]: T = tuple() #创建一个空的元组 In [4]: TOut[4]: () In [8]: T = (1,) #创建包含一个对象的元组,不能写成T = (1),这会被解释成T = 1 In...
-
In [1]: T = () #创建一个空的元组
-
-
In [2]: T
-
Out[2]: ()
-
-
In [3]: T = tuple() #创建一个空的元组
-
-
In [4]: T
-
Out[4]: ()
-
-
In [8]: T = (1,) #创建包含一个对象的元组,不能写成T = (1),这会被解释成T = 1
-
-
In [9]: T
-
Out[9]: (1,)
-
-
In [10]: T = (1,2,3) #可以这样写
-
-
In [11]: T
-
Out[11]: (1, 2, 3)
-
-
-
In [12]: T = 1,2,3,4,5 #也可以这样写
-
-
In [13]: T
-
Out[13]: (1, 2, 3, 4, 5)
-
In [1]: a,b = 1,2
-
-
In [2]: a
-
Out[2]: 1
-
-
In [3]: b
-
Out[3]: 2
-
In [4]: a,b = b,a
-
-
In [5]: a
-
Out[5]: 2
-
-
In [6]: b
-
Out[6]: 1
-
-
还可以这样写
-
In [17]: a,*b = 1,2,3
-
-
In [18]: a
-
Out[18]: 1
-
-
In [19]: b
-
Out[19]: [2, 3]
-
In [1]: T = tuple()
-
-
In [2]: T.__len__() #可以调用元组的各种方法
-
Out[2]: 0
-
In [3]: s = "123"
-
-
In [4]: T = tuple(s)
-
-
In [5]: T
-
Out[5]: ('1', '2', '3')
-
-
In [6]: s #s本身不会改变
-
Out[6]: '123'
-
-
-
-
In [15]: t = tuple((1,2))
-
-
In [16]: t
-
Out[16]: (1, 2)
-
In [1]: T = (1,2)
-
-
In [2]: T.__add__((3,4))
-
Out[2]: (1, 2, 3, 4)
-
-
In [3]: T.__add__(3) #参数必须是元组,否则报错
-
---------------------------------------------------------------------------
-
TypeError Traceback (most recent call last)
-
<ipython-input-3-c1bfa90c64b7> in <module>()
-
----> 1 T.__add__(3)
-
-
TypeError: can only concatenate tuple (not "int") to tuple
-
In [1]: T = 1,2,'abc',[2,3]
-
-
In [2]: T.__contains__(1)
-
Out[2]: True
-
-
In [3]: T.__contains__([2,3])
-
Out[3]: True
-
-
In [4]: T.__contains__([2])
-
Out[4]: False
-
-
-
相当于使用in
-
In [5]: 1 in T
-
Out[5]: True
-
-
-
In [6]: [2,3] in T
-
Out[6]: True
-
-
In [7]: [2] in T
-
Out[7]: False
-
In [1]: T = 1,2,'abc',[2,3]
-
-
In [2]: T.__eq__(5) #参数不是元组不能比较,但也不报错
-
Out[2]: NotImplemented
-
-
In [3]: T.__eq__((5,6)) #参数是元组可以比较是否相等
-
Out[3]: False
-
-
In [4]: T.__eq__([5,6]) #参数不是元组不能比较,但也不报错
-
Out[4]: NotImplemented
-
In [5]: T.__getattribute__('__gt__') __gt__ 是T的属性
-
Out[5]: <method-wrapper '__gt__' of tuple object at 0x024F9660>
-
-
In [6]: T.__getattribute__('gt') gt不是T的属性,报错
-
---------------------------------------------------------------------------
-
AttributeError Traceback (most recent call last)
-
<ipython-input-6-c1ee4b843234> in <module>()
-
----> 1 T.__getattribute__('gt')
-
-
AttributeError: 'tuple' object has no attribute 'gt'
-
In [7]: T = 1,2,'abc',[2,3]
-
-
In [8]: T.__getitem__(2) #获取该元组的第二个对象
-
Out[8]: 'abc'
-
-
In [9]: T[2]
-
Out[9]: 'abc'
-
-
In [10]: T[6] #越界报错
-
---------------------------------------------------------------------------
-
IndexError Traceback (most recent call last)
-
<ipython-input-10-bec53b80c70a> in <module>()
-
----> 1 T[6]
-
-
IndexError: tuple index out of range
-
-
In [11]: T[-1] #获取该元组的最后一个对象
-
Out[11]: [2, 3]
-
In [17]: t = T.__getnewargs__()
-
-
In [18]: type(t)
-
Out[18]: tuple
-
-
In [19]: t
-
Out[19]: ((1, 2, 'abc', [2, 3]),)
-
-
In [20]: len(t)
-
Out[20]: 1
-
-
In [21]: help(T.__getnewargs__)
-
Help on built-in function __getnewargs__:
-
-
__getnewargs__(...) method of builtins.tuple instance
-
In [22]: T = (1,2,3)
-
-
In [23]: T.__hash__()
-
Out[23]: -378539185
-
-
In [24]: hash(T)
-
Out[24]: -378539185
-
In [25]: T = (1,2,'3')
-
-
In [26]: T.__iter__()
-
Out[26]: <tuple_iterator at 0x361f390>
-
-
In [27]: for i in T.__iter__():
-
...: print (i)
-
...:
-
1
-
2
-
3
-
In [28]: T = (1,2,'abcdef',[2,3])
-
-
In [29]: T.__len__()
-
Out[29]: 4
-
-
In [30]: len(T)
-
Out[30]: 4
-
In [31]: T = (1,2,'3')
-
-
In [32]: T.__mul__(2)
-
Out[32]: (1, 2, '3', 1, 2, '3')
-
-
In [34]: T.__mul__([2])
-
---------------------------------------------------------------------------
-
TypeError Traceback (most recent call last)
-
<ipython-input-34-cf710bf97ffd> in <module>()
-
----> 1 T.__mul__([2])
-
-
TypeError: 'list' object cannot be interpreted as an integer
-
In [1]: T = (1,2,'3')
-
-
In [2]: T.__repr__()
-
Out[2]: "(1, 2, '3')"
-
-
In [3]: type(T.__repr__())
-
Out[3]: str
-
In [4]: T
-
Out[4]: (1, 2, '3')
-
-
In [5]: T.__rmul__(5)
-
Out[5]: (1, 2, '3', 1, 2, '3', 1, 2, '3', 1, 2, '3', 1, 2, '3')
-
-
In [6]: T.__rmul__('5')
-
---------------------------------------------------------------------------
-
TypeError Traceback (most recent call last)
-
<ipython-input-6-e2885e94a711> in <module>()
-
----> 1 T.__rmul__('5')
-
-
TypeError: 'str' object cannot be interpreted as an integer
-
In [1]: T = (1,2,1,1,2,2,3,'3',[4,5])
-
-
In [2]: T.count(1)
-
Out[2]: 3
-
-
In [3]: T.count(2)
-
Out[3]: 3
-
-
In [4]: T.count(3)
-
Out[4]: 1
-
-
In [5]: T.count('3')
-
Out[5]: 1
-
-
In [6]: T.count('4')
-
Out[6]: 0
-
In [12]: T
-
Out[12]: (1, 2, 1, 1, 2, 2, 3, '3', [4, 5])
-
-
In [13]: T.index(1) #返回第一个出现对象的位置
-
Out[13]: 0
-
-
In [14]: T.index('3') #返回第一个出现对象的位置
-
Out[14]: 7
-
-
In [15]: T.index(0) #元组中不存在是时报错
-
---------------------------------------------------------------------------
-
ValueError Traceback (most recent call last)
-
<ipython-input-15-768d2cfd9263> in <module>()
-
----> 1 T.index(0)
-
-
ValueError: tuple.index(x): x not in tuple
文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq523176585/article/details/77983245
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)