Python的元组学习(五)

本节来学习python的元组,在python语言中,元组的关键字是tuple同时元组是不可变的,列表与字典是可变的,元组

的定义是一个(),下面通过代码我们具体来看元组对象的类所具备的功能和查看元组帮助详细的信息:

#!/usr/bin/env python
#coding:utf-8

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

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

复制代码
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,下面我们来看元组常用方法的具体使用,通过实例的代码来看元组类中的方法的使用,见案例的代码:

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

    我们知道,元组是不可变的,那么元组里面嵌套的列表,字典可以改变内容吗?答案是可以的,下面

的代码就来实现这部分,见实现的代码:

复制代码
 1 #!/usr/bin/env python
 2 #coding:utf-8
 3 
 4 tuple1=(123,'wuya',[11,22,33,44,55],{'name':'wuya','age':20})
 5 
 6 #修改嵌套在元组里面的列表内容
 7 tuple1[2][0]=u'无涯'
 8 print u'元组中的列表被修改后的内容:',tuple1,u'类型为:',type(tuple1)
 9 
10 #修改嵌套在元组里面的字典内容
11 tuple1[3]['name']='无涯'
12 print u'元组中的字典被修改后的内容L',tuple1,u'类型为:',type(tuple1)
复制代码

猜你喜欢

转载自blog.csdn.net/yyang3121/article/details/80521682