国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python數(shù)據(jù)類(lèi)型詳解(三)元祖:tuple

2019-11-25 16:48:09
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一.基本數(shù)據(jù)類(lèi)型

  整數(shù):int
  字符串:str(注:/t等于一個(gè)tab鍵)
  布爾值: bool
  列表:list
  列表用[]
  元祖:tuple
  元祖用()

  字典:dict
注:所有的數(shù)據(jù)類(lèi)型都存在想對(duì)應(yīng)的類(lèi)列里,元祖和列表功能一樣,列表可以修改,元祖不能修改。

二.列表所有數(shù)據(jù)類(lèi)型:

基本操作:

索引,切片,長(zhǎng)度,包含,循環(huán)

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.  """  def count(self, value): # real signature unknown; restored from __doc__    """ T.count(value) -> integer -- return number of occurrences of value """    (T.count(價(jià)值)- >整數(shù),返回值的出現(xiàn)次數(shù))    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.    """    (T。指數(shù)(價(jià)值,[開(kāi)始,[不要]])- >整數(shù),返回第一索引值。提出了ValueError如果不存在的價(jià)值。)    return 0  def __add__(self, *args, **kwargs): # real signature unknown    """ Return self+value. """    pass  def __contains__(self, *args, **kwargs): # real signature unknown    """ Return key in self. """    pass  def __eq__(self, *args, **kwargs): # real signature unknown    """ Return self==value. """    pass  def __getattribute__(self, *args, **kwargs): # real signature unknown    """ Return getattr(self, name). """    pass  def __getitem__(self, *args, **kwargs): # real signature unknown    """ Return self[key]. """    pass  def __getnewargs__(self, *args, **kwargs): # real signature unknown    pass  def __ge__(self, *args, **kwargs): # real signature unknown    """ Return self>=value. """    pass  def __gt__(self, *args, **kwargs): # real signature unknown    """ Return self>value. """    pass  def __hash__(self, *args, **kwargs): # real signature unknown    """ Return hash(self). """    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, *args, **kwargs): # real signature unknown    """ Implement iter(self). """    pass  def __len__(self, *args, **kwargs): # real signature unknown    """ Return len(self). """    pass  def __le__(self, *args, **kwargs): # real signature unknown    """ Return self<=value. """    pass  def __lt__(self, *args, **kwargs): # real signature unknown    """ Return self<value. """    pass  def __mul__(self, *args, **kwargs): # real signature unknown    """ Return self*value.n """    pass  @staticmethod # known case of __new__  def __new__(*args, **kwargs): # real signature unknown    """ Create and return a new object. See help(type) for accurate signature. """    pass  def __ne__(self, *args, **kwargs): # real signature unknown    """ Return self!=value. """    pass  def __repr__(self, *args, **kwargs): # real signature unknown    """ Return repr(self). """    pass  def __rmul__(self, *args, **kwargs): # real signature unknown    """ Return self*value. """    pass

三.所有元祖數(shù)據(jù)類(lèi)型舉例

#count 用于計(jì)算元素出現(xiàn)的個(gè)數(shù)name_tuple = ("zhangyanlin","suoning","nick")print(name_tuple.count('zhangyanlin')) #index獲取指定元素的指定位置name_tuple = ("zhangyanlin","suoning","nick")print(name_tuple.index('zhangyanlin'))

四.索引

name_tuple = ("zhangyanlin","suoning","nick")print(name_tuple[1])

五.切片

#取出第一位到最后一位減1的元素name_tuple = ("zhangyanlin","suoning","nick")print(name_tuple[0:len(name_tuple)-1])

六.總長(zhǎng)度len

#取出最后一位減1的元素name_tuple = ("zhangyanlin","suoning","nick")print(name_tuple[len(name_tuple)-1])

七.for循環(huán)

name_tuple = ("zhangyanlin","suoning","nick")for i in name_tuple:  print(i)

那么使用 tuple 有什么好處呢?

Tuple 比 list 操作速度快。如果您定義了一個(gè)值的常量集,并且唯一要用它做的是不斷地遍歷它,請(qǐng)使用 tuple 代替 list。
如果對(duì)不需要修改的數(shù)據(jù)進(jìn)行 “寫(xiě)保護(hù)”,可以使代碼更安全。使用 tuple 而不是 list 如同擁有一個(gè)隱含的 assert 語(yǔ)句,說(shuō)明這一數(shù)據(jù)是常量。如果必須要改變這些值,則需要執(zhí)行 tuple 到 list 的轉(zhuǎn)換 (需要使用一個(gè)特殊的函數(shù))。

還記得我說(shuō)過(guò) dictionary keys 可以是字符串,整數(shù)和 “其它幾種類(lèi)型”嗎?Tuples 就是這些類(lèi)型之一。Tuples 可以在 dictionary 中被用做 key,但是 list 不行。實(shí)際上,事情要比這更復(fù)雜。Dictionary key 必須是不可變的。Tuple 本身是不可改變的,但是如果您有一個(gè) list 的 tuple,那就認(rèn)為是可變的了,用做 dictionary key 就是不安全的。只有字符串、整數(shù)或其它對(duì) dictionary 安全的 tuple 才可以用作 dictionary key。

Tuples 可以用在字符串格式化中,我們會(huì)很快看到。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 卢龙县| 砚山县| 溆浦县| 宾阳县| 东乡县| 平谷区| 昌平区| 开封市| 章丘市| 宜春市| 剑川县| 安图县| 荥经县| 裕民县| 宁德市| 定结县| 阿合奇县| 桃源县| 深水埗区| 永昌县| 洮南市| 乌兰浩特市| 来宾市| 华池县| 安宁市| 贺州市| 林甸县| 海门市| 西吉县| 湘阴县| 茶陵县| 山阳县| 中阳县| 上虞市| 崇仁县| 西城区| 合山市| 鹿泉市| 三台县| 白城市| 舞钢市|