前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python的元组学习(五)

Python的元组学习(五)

作者头像
无涯WuYa
发布2018-10-25 16:05:29
5000
发布2018-10-25 16:05:29
举报

本节来学习python的元组,在python语言中,元组的关键字是tuple同时元组是不可变的,列表与字典是可变的,元组的定义是一个(),下面通过代码我们具体来看元组对象的类所具备的功能和查看元组帮助详细的信息:

代码语言:javascript
复制
#!/usr/bin/env python
#coding:utf-8

tuple1=('123','456')
print u'查看元组对象类的功能:',dir(tuple1)
print u'查看元组帮助的详细信息:',help(type(tuple1))

见如上代码执行后的输出内容:

代码语言:javascript
复制
C:\Python27\python.exe D:/git/Python/FullStack/Study/index.py
查看元组对象类的功能: ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
查看元组帮助的详细信息:Help on class tuple in module __builtin__: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. |  
 |  Methods defined here: |  
 |  __add__(...) |      x.__add__(y) <==> x+y |  
 |  __contains__(...) |      x.__contains__(y) <==> y in x |  
 |  __eq__(...) |      x.__eq__(y) <==> x==y |  
 |  __ge__(...) |      x.__ge__(y) <==> x>=y |  
 |  __getattribute__(...) |      x.__getattribute__('name') <==> x.name |  
 |  __getitem__(...) |      x.__getitem__(y) <==> x[y] |  
 |  __getnewargs__(...) |  
 |  __getslice__(...) |      x.__getslice__(i, j) <==> x[i:j] |      
 |      Use of negative indices is not supported. |  
 |  __gt__(...) |      x.__gt__(y) <==> x>y |  
 |  __hash__(...) |      x.__hash__() <==> hash(x) |  
 |  __iter__(...) |      x.__iter__() <==> iter(x) |  
 |  __le__(...) |      x.__le__(y) <==> x<=y |  
 |  __len__(...) |      x.__len__() <==> len(x) |  
 |  __lt__(...) |      x.__lt__(y) <==> x<y |  
 |  __mul__(...) |      x.__mul__(n) <==> x*n |  
 |  __ne__(...) |      x.__ne__(y) <==> x!=y |  
 |  __repr__(...) |      x.__repr__() <==> repr(x) |  
 |  __rmul__(...) |      x.__rmul__(n) <==> n*x |  
 |  count(...) |      T.count(value) -> integer -- return number of occurrences of value |  
 |  index(...) |      T.index(value, [start, [stop]]) -> integer -- return first index of value. |      Raises ValueError if the value is not present. |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here: |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

None

Process finished with exit code 0

OK,下面我们来看元组常用方法的具体使用,通过实例的代码来看元组类中的方法的使用,见案例的代码:

代码语言:javascript
复制
#!/usr/bin/env python
#coding:utf-8 
tuple1=(123,'wuya',[11,22,33,44,55],{'name':'wuya','age':20})
#使用索引取出元组具体的值
 print u'取出元组中最后一个元素的值:',tuple1[3] 
 #取出元组的最后一位元素的写法是: 
 print tuple1[len(tuple1)-1] 
 #切片在元组中的使用,见如下的代码
 print tuple1[0:3]
#使用循环的方式取出元组中所有的值
for item in tuple1: 
    print item
#获取元组中指定元素的索引
print u"{'name':'wuya','age':20}在元组中的索引为:",tuple1.index("wuya")
#获取元组中的某个元素在元组中的个数
print tuple1.count('wuya')

我们知道,元组是不可变的,那么元组里面嵌套的列表,字典可以改变内容吗?答案是可以的,下面的代码就来实现这部分,见实现的代码:

#!/usr/bin/env python

#coding:utf-8

tuple1=(123,'wuya',[11,22,33,44,55],{'name':'wuya','age':20})

#修改嵌套在元组里面的列表内容

tuple1[2][0]=u'无涯'

print u'元组中的列表被修改后的内容:',tuple1,u'类型为:',type(tuple1)

#修改嵌套在元组里面的字典内容

tuple1[3]['name']='无涯'

print u'元组中的字典被修改后的内容L',tuple1,u'类型为:',type(tuple1)

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-07-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档