第十五节,基本数据类型,元组tuple

元组和列表的区别

  元组和列表几乎是一样的

  不一样的地方就是元组创建后元组的元素不可以修改,比如(添加,拓展,移除等修改功能,但是元组里的元素的元素是可以修改的)

基本操作:
  索引
  切片
  循环
  长度
  包含

 

创建元组

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 a = ("lyh", "guixiu", "xioum")
4 #或者
5 b = tuple(("lyh", "guixiu", "xioum"))

 

tuple转换元组
"""(转换成元组,需要转换的可迭代数据变量) 注意:能转换成元组的必须是可迭代的,也就是可以被for循环的"""
字符串,字典,列表 > 都可以转换成元组,转换成元组都是可以被for循环的,for循环每次循环到的数据就是元组的一个元素

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 a = "小鸡炖蘑菇"
4 b = tuple(a)
5 print(b)
6 #输出 ('小', '鸡', '炖', '蘑', '菇')

 


索引

  格式:元组变量加[索引下标] 的方式

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 a = ("lyh", "guixiu", "xioum")
4 print(a[1])
5 #打印出 guixiu 打印出元素下标为1的元素

切片
  格式:元组变量加[起始下标:结束下标]

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 a = ("lyh", "guixiu", "xioum")
4 print(a[0:3])
5 #打印出 ('lyh', 'guixiu', 'xioum') 打印出元素下标0到3的元素

  len(p_object)

  """(统计元组里的元素数量)"""

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 a = ("lyh", "guixiu", "xioum")
4 print(len(a))
5 #打印出 3 统计出元组里有3个元素

  while循环

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #while循环
4 a = ("lyh", "guixiu", "xioum")
5 b = 0
6 while b < len(a):
7     print(a[b])
8     b += 1
9 #循环出元组里的所有元素

  for循环

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #for循环
4 a = ("lyh", "guixiu", "xioum")
5 for b in a: #b为自定义循环变量
6     print(b)
7 #循环出元组里的所有元素

  count(self, value)

  """(计算元素在元组里出现的次数)"""要计算的元素

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 a = ("lyh", "guixiu", "xioum")
4 print(a.count("guixiu"))
5 #打印出 1 guixiu元素在元组里出现一次

  index(self, value, start=None, stop=None)

  """(获取指定元素在元组里的索引位置)"""要查找的元素,起始位置,结束位置

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 a = ("lyh", "guixiu", "xioum")
4 print(a.index("guixiu"))
5 #打印出 1 guixiu元素在元组里的索引位置是1,默认从0开始计算
lass 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.
    """
    def count(self, value): # real signature unknown; restored from __doc__
        """ T.count(value) -> integer -- return number of occurrences of value """
        return 0

    def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
        """
        T.index(value, [start, [stop]]) -> integer -- return first index of value.
        Raises ValueError if the value is not present.
        """
        return 0

    def __add__(self, y): # real signature unknown; restored from __doc__
        """ x.__add__(y) <==> x+y """
        pass

    def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x """
        pass

    def __eq__(self, y): # real signature unknown; restored from __doc__
        """ x.__eq__(y) <==> x==y """
        pass

    def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__('name') <==> x.name """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __getnewargs__(self, *args, **kwargs): # real signature unknown
        pass

    def __getslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__getslice__(i, j) <==> x[i:j]
                   
                   Use of negative indices is not supported.
        """
        pass

    def __ge__(self, y): # real signature unknown; restored from __doc__
        """ x.__ge__(y) <==> x>=y """
        pass

    def __gt__(self, y): # real signature unknown; restored from __doc__
        """ x.__gt__(y) <==> x>y """
        pass

    def __hash__(self): # real signature unknown; restored from __doc__
        """ x.__hash__() <==> hash(x) """
        pass

    def __init__(self, seq=()): # known special case of tuple.__init__
        """
        tuple() -> empty tuple
        tuple(iterable) -> tuple initialized from iterable's items
        
        If the argument is a tuple, the return value is the same object.
        # (copied from class doc)
        """
        pass

    def __iter__(self): # real signature unknown; restored from __doc__
        """ x.__iter__() <==> iter(x) """
        pass

    def __len__(self): # real signature unknown; restored from __doc__
        """ x.__len__() <==> len(x) """
        pass

    def __le__(self, y): # real signature unknown; restored from __doc__
        """ x.__le__(y) <==> x<=y """
        pass

    def __lt__(self, y): # real signature unknown; restored from __doc__
        """ x.__lt__(y) <==> x<y """
        pass

    def __mul__(self, n): # real signature unknown; restored from __doc__
        """ x.__mul__(n) <==> x*n """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more): # real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __ne__(self, y): # real signature unknown; restored from __doc__
        """ x.__ne__(y) <==> x!=y """
        pass

    def __repr__(self): # real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass

    def __rmul__(self, n): # real signature unknown; restored from __doc__
        """ x.__rmul__(n) <==> n*x """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ T.__sizeof__() -- size of T in memory, in bytes """
        pass
tuple

 

元组里的元素的元素追加
元组的元素是不可修改和和追加的,也就是元组的子级不可修改,元组的元素的元素也就是孙级是可以修改的

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #元组里的元素的元素追加
 4 #元组的元素是不可修改和和追加的,也就是元组的子级不可修改,元组的元素的元素也就是孙级是可以修改的
 5 #追加方式
 6 #列1
 7 a = (11,22,["guixiu",{"k1":"k2"}])
 8 b = {"k3":"k4"}
 9 a[2][1].update(b)#索引到元组里字典时,将b元组最佳进去
10 print(a)
11 #输出  (11, 22, ['guixiu', {'k1': 'k2', 'k3': 'k4'}])
12 
13 #列2
14 a = (11,22,["guixiu",{"k1":"k2"}])
15 c = a[2][1]#索引到元组里的字典
16 c["k3"] = "k4"
17 print(a)
18 #输出  (11, 22, ['guixiu', {'k1': 'k2', 'k3': 'k4'}])

 

元组的功能

转换列表
索引
切片
for循环
长度
反转
排序
索引位置
统计元素个数

posted @ 2016-06-02 20:17  林贵秀  阅读(406)  评论(0编辑  收藏  举报