第十五節,基本數據類型,元組tuple

元組和列表的區別python

  元組和列表幾乎是同樣的ide

  不同的地方就是元組建立後元組的元素不能夠修改,好比(添加,拓展,移除等修改功能,可是元組裏的元素的元素是能夠修改的)ui

基本操做:
  索引
  切片
  循環
  長度
  包含
spa

 

建立元組rest

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循環每次循環到的數據就是元組的一個元素code

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

 


索引blog

  格式:元組變量加[索引下標] 的方式排序

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)utf-8

  """(統計元組裏的元素數量)"""

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循環長度反轉排序索引位置統計元素個數

相關文章
相關標籤/搜索