码迷,mamicode.com
首页 > 编程语言 > 详细

Python 列表、元组、字典

时间:2018-03-19 10:19:57      阅读:174      评论:0      收藏:0      [点我收藏+]

标签: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 列表、元组、字典

标签:python

原文地址:http://blog.51cto.com/395469372/2088353

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!