Python学习笔记(二):tuple属性及方法浅析

举报
悦来客栈的老板 发表于 2020/12/29 01:35:20 2020/12/29
【摘要】 元组是存放任意元素集合,不能修改其内容. 简单创建元组 In [1]: T = () #创建一个空的元组 In [2]: TOut[2]: () In [3]: T = tuple() #创建一个空的元组 In [4]: TOut[4]: () In [8]: T = (1,) #创建包含一个对象的元组,不能写成T = (1),这会被解释成T = 1 In...






  
  1. In [1]: T = () #创建一个空的元组
  2. In [2]: T
  3. Out[2]: ()
  4. In [3]: T = tuple() #创建一个空的元组
  5. In [4]: T
  6. Out[4]: ()
  7. In [8]: T = (1,) #创建包含一个对象的元组,不能写成T = (1),这会被解释成T = 1
  8. In [9]: T
  9. Out[9]: (1,)
  10. In [10]: T = (1,2,3) #可以这样写
  11. In [11]: T
  12. Out[11]: (1, 2, 3)
  13. In [12]: T = 1,2,3,4,5 #也可以这样写
  14. In [13]: T
  15. Out[13]: (1, 2, 3, 4, 5)







  
  1. In [1]: a,b = 1,2
  2. In [2]: a
  3. Out[2]: 1
  4. In [3]: b
  5. Out[3]: 2








  
  1. In [4]: a,b = b,a
  2. In [5]: a
  3. Out[5]: 2
  4. In [6]: b
  5. Out[6]: 1
  6. 还可以这样写
  7. In [17]: a,*b = 1,2,3
  8. In [18]: a
  9. Out[18]: 1
  10. In [19]: b
  11. Out[19]: [2, 3]
























































































  
  1. In [1]: T = tuple()
  2. In [2]: T.__len__() #可以调用元组的各种方法
  3. Out[2]: 0









  
  1. In [3]: s = "123"
  2. In [4]: T = tuple(s)
  3. In [5]: T
  4. Out[5]: ('1', '2', '3')
  5. In [6]: s #s本身不会改变
  6. Out[6]: '123'
  7. In [15]: t = tuple((1,2))
  8. In [16]: t
  9. Out[16]: (1, 2)









  
  1. In [1]: T = (1,2)
  2. In [2]: T.__add__((3,4))
  3. Out[2]: (1, 2, 3, 4)
  4. In [3]: T.__add__(3) #参数必须是元组,否则报错
  5. ---------------------------------------------------------------------------
  6. TypeError Traceback (most recent call last)
  7. <ipython-input-3-c1bfa90c64b7> in <module>()
  8. ----> 1 T.__add__(3)
  9. TypeError: can only concatenate tuple (not "int") to tuple









  
  1. In [1]: T = 1,2,'abc',[2,3]
  2. In [2]: T.__contains__(1)
  3. Out[2]: True
  4. In [3]: T.__contains__([2,3])
  5. Out[3]: True
  6. In [4]: T.__contains__([2])
  7. Out[4]: False
  8. 相当于使用in
  9. In [5]: 1 in T
  10. Out[5]: True
  11. In [6]: [2,3] in T
  12. Out[6]: True
  13. In [7]: [2] in T
  14. Out[7]: False








  
  1. In [1]: T = 1,2,'abc',[2,3]
  2. In [2]: T.__eq__(5) #参数不是元组不能比较,但也不报错
  3. Out[2]: NotImplemented
  4. In [3]: T.__eq__((5,6)) #参数是元组可以比较是否相等
  5. Out[3]: False
  6. In [4]: T.__eq__([5,6]) #参数不是元组不能比较,但也不报错
  7. Out[4]: NotImplemented












  
  1. In [5]: T.__getattribute__('__gt__') __gt__ 是T的属性
  2. Out[5]: <method-wrapper '__gt__' of tuple object at 0x024F9660>
  3. In [6]: T.__getattribute__('gt') gt不是T的属性,报错
  4. ---------------------------------------------------------------------------
  5. AttributeError Traceback (most recent call last)
  6. <ipython-input-6-c1ee4b843234> in <module>()
  7. ----> 1 T.__getattribute__('gt')
  8. AttributeError: 'tuple' object has no attribute 'gt'










  
  1. In [7]: T = 1,2,'abc',[2,3]
  2. In [8]: T.__getitem__(2) #获取该元组的第二个对象
  3. Out[8]: 'abc'
  4. In [9]: T[2]
  5. Out[9]: 'abc'
  6. In [10]: T[6] #越界报错
  7. ---------------------------------------------------------------------------
  8. IndexError Traceback (most recent call last)
  9. <ipython-input-10-bec53b80c70a> in <module>()
  10. ----> 1 T[6]
  11. IndexError: tuple index out of range
  12. In [11]: T[-1] #获取该元组的最后一个对象
  13. Out[11]: [2, 3]







  
  1. In [17]: t = T.__getnewargs__()
  2. In [18]: type(t)
  3. Out[18]: tuple
  4. In [19]: t
  5. Out[19]: ((1, 2, 'abc', [2, 3]),)
  6. In [20]: len(t)
  7. Out[20]: 1
  8. In [21]: help(T.__getnewargs__)
  9. Help on built-in function __getnewargs__:
  10. __getnewargs__(...) method of builtins.tuple instance













  
  1. In [22]: T = (1,2,3)
  2. In [23]: T.__hash__()
  3. Out[23]: -378539185
  4. In [24]: hash(T)
  5. Out[24]: -378539185










  
  1. In [25]: T = (1,2,'3')
  2. In [26]: T.__iter__()
  3. Out[26]: <tuple_iterator at 0x361f390>
  4. In [27]: for i in T.__iter__():
  5. ...: print (i)
  6. ...:
  7. 1
  8. 2
  9. 3













  
  1. In [28]: T = (1,2,'abcdef',[2,3])
  2. In [29]: T.__len__()
  3. Out[29]: 4
  4. In [30]: len(T)
  5. Out[30]: 4











  
  1. In [31]: T = (1,2,'3')
  2. In [32]: T.__mul__(2)
  3. Out[32]: (1, 2, '3', 1, 2, '3')
  4. In [34]: T.__mul__([2])
  5. ---------------------------------------------------------------------------
  6. TypeError Traceback (most recent call last)
  7. <ipython-input-34-cf710bf97ffd> in <module>()
  8. ----> 1 T.__mul__([2])
  9. TypeError: 'list' object cannot be interpreted as an integer

















  
  1. In [1]: T = (1,2,'3')
  2. In [2]: T.__repr__()
  3. Out[2]: "(1, 2, '3')"
  4. In [3]: type(T.__repr__())
  5. Out[3]: str









  
  1. In [4]: T
  2. Out[4]: (1, 2, '3')
  3. In [5]: T.__rmul__(5)
  4. Out[5]: (1, 2, '3', 1, 2, '3', 1, 2, '3', 1, 2, '3', 1, 2, '3')
  5. In [6]: T.__rmul__('5')
  6. ---------------------------------------------------------------------------
  7. TypeError Traceback (most recent call last)
  8. <ipython-input-6-e2885e94a711> in <module>()
  9. ----> 1 T.__rmul__('5')
  10. TypeError: 'str' object cannot be interpreted as an integer










  
  1. In [1]: T = (1,2,1,1,2,2,3,'3',[4,5])
  2. In [2]: T.count(1)
  3. Out[2]: 3
  4. In [3]: T.count(2)
  5. Out[3]: 3
  6. In [4]: T.count(3)
  7. Out[4]: 1
  8. In [5]: T.count('3')
  9. Out[5]: 1
  10. In [6]: T.count('4')
  11. Out[6]: 0









  
  1. In [12]: T
  2. Out[12]: (1, 2, 1, 1, 2, 2, 3, '3', [4, 5])
  3. In [13]: T.index(1) #返回第一个出现对象的位置
  4. Out[13]: 0
  5. In [14]: T.index('3') #返回第一个出现对象的位置
  6. Out[14]: 7
  7. In [15]: T.index(0) #元组中不存在是时报错
  8. ---------------------------------------------------------------------------
  9. ValueError Traceback (most recent call last)
  10. <ipython-input-15-768d2cfd9263> in <module>()
  11. ----> 1 T.index(0)
  12. ValueError: tuple.index(x): x not in tuple








文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/77983245

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。