Python 列表、元組、字典

來源:互聯網
上載者:User

標籤:python

列表的方法:

        help(list.append)

        append      L.append(object) -- append object to end   追加元素

                    

remove 刪除第一次出現的元素

    L.remove(value) -- remove first occurrence of value.

    Raises ValueError if the value is not present.

count        L.count(value) -> integer -- return number of occurrences of value  元素在列表中出現的個數

          

reverse      L.reverse() -- reverse *IN PLACE*  倒序

            

extend      L.extend(iterable) -- extend list by appending elements from the iterable 追加list

       

sort  排序

    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;

    cmp(x, y) -> -1, 0, 1

index  返回元素的位置,無則拋異常

           L.index(value, [start, [stop]]) -> integer -- return first index of value.

           Raises ValueError if the value is not present.

insert      L.insert(index, object) -- insert object before index

pop

                  L.pop([index]) -> item -- remove and return item at index (default last). 返回最後一個元素,並刪除

                    Raises IndexError if list is empty or index is out of range.

    列表的切片

       a[1:]            #片段操作符

       [1,2]+[3,4]    #同extend()

      [2]*4             #為 [2,2,2,2]

     del l[2]            #刪除指定下標的元素

     del list[1:3]   #刪除指定下標範圍的元素

     list的複製

     list1=list2 #list2的別名,對list1操作,即對list2操作

     list1=list2[:]  #產生list2的一個copy




元組:

  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.




字典

dict.clear()  -> D.clear() -> None.  Remove all items from D. 

dict.copy()  -> D.copy() -> a shallow copy of D  

dict.fromkeys()  dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.

                    v defaults to None.

dict.get()    D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.    

dict.has_key()  D.has_key(k) -> True if D has a key k, else False

dict.items()    D.items() -> list of D's (key, value) pairs, as 2-tuples

dict.iteritems()      D.iteritems() -> an iterator over the (key, value) items of D

dict.iterkeys()      D.iterkeys() -> an iterator over the keys of D

dict.itervalues()    D.itervalues() -> an iterator over the values of D

dict.keys()      D.keys() -> list of D's keys 

dict.mro()      mro() -> list

                return a type's method resolution order

dict.pop()    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.

    If key is not found, d is returned if given, otherwise KeyError is raised

dict.popitem()  

      D.popitem() -> (k, v), remove and return some (key, value) pair as a

    2-tuple; but raise KeyError if D is empty.

dict.setdefault()

      D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

dict.update() 

      update(...)

    D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.

    If E present and has a .keys() method, does:    for k in E: D[k] = E[k]

    If E present and lacks .keys() method, does:    for (k, v) in E: D[k] = v

    In either case, this is followed by: for k in F: D[k] = F[k]

    

dict.values()      D.values() -> list of D's values  

dict.viewitems()      D.viewitems() -> a set-like object providing a view on D's items

dict.viewkeys()      D.viewkeys() -> a set-like object providing a view on D's keys

dict.viewvalues()    D.viewvalues() -> an object providing a view on D's values




Python 列表、元組、字典

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.