首页 > 代码库 > 字典丶列表丶元组的一些常用操作

字典丶列表丶元组的一些常用操作

 

1.字典(无序)

1.创建字典:

1 person = {"name": "mr.wu", age: 18}
2 3 person = dict({"name": "mr.wu", age: 18})
4 5 person = dict(name="mr.wu",age="18")        

2.字典的常用操作  

  • 索引
  • 新增
  • 删除
  • 键、值、键值对
  • 循环
  • 长度
  • 技术分享
      1 class dict(object):
      2     """
      3     dict() -> new empty dictionary
      4     dict(mapping) -> new dictionary initialized from a mapping object‘s
      5         (key, value) pairs
      6     dict(iterable) -> new dictionary initialized as if via:
      7         d = {}
      8         for k, v in iterable:
      9             d[k] = v
     10     dict(**kwargs) -> new dictionary initialized with the name=value pairs
     11         in the keyword argument list.  For example:  dict(one=1, two=2)
     12     """
     13 
     14     def clear(self): # real signature unknown; restored from __doc__
     15         """ 清除内容 """
     16         """ D.clear() -> None.  Remove all items from D. """
     17         pass
     18 
     19     def copy(self): # real signature unknown; restored from __doc__
     20         """ 浅拷贝 """
     21         """ D.copy() -> a shallow copy of D """
     22         pass
     23 
     24     @staticmethod # known case
     25     def fromkeys(S, v=None): # real signature unknown; restored from __doc__
     26         """
     27         dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
     28         v defaults to None.
     29         """
     30         pass
     31 
     32     def get(self, k, d=None): # real signature unknown; restored from __doc__
     33         """ 根据key获取值,d是默认值 """
     34         """ D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None. """
     35         pass
     36 
     37     def has_key(self, k): # real signature unknown; restored from __doc__
     38         """ 是否有key """
     39         """ D.has_key(k) -> True if D has a key k, else False """
     40         return False
     41 
     42     def items(self): # real signature unknown; restored from __doc__
     43         """ 所有项的列表形式 """
     44         """ D.items() -> list of D‘s (key, value) pairs, as 2-tuples """
     45         return []
     46 
     47     def iteritems(self): # real signature unknown; restored from __doc__
     48         """ 项可迭代 """
     49         """ D.iteritems() -> an iterator over the (key, value) items of D """
     50         pass
     51 
     52     def iterkeys(self): # real signature unknown; restored from __doc__
     53         """ key可迭代 """
     54         """ D.iterkeys() -> an iterator over the keys of D """
     55         pass
     56 
     57     def itervalues(self): # real signature unknown; restored from __doc__
     58         """ value可迭代 """
     59         """ D.itervalues() -> an iterator over the values of D """
     60         pass
     61 
     62     def keys(self): # real signature unknown; restored from __doc__
     63         """ 所有的key列表 """
     64         """ D.keys() -> list of D‘s keys """
     65         return []
     66 
     67     def pop(self, k, d=None): # real signature unknown; restored from __doc__
     68         """ 获取并在字典中移除 """
     69         """
     70         D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
     71         If key is not found, d is returned if given, otherwise KeyError is raised
     72         """
     73         pass
     74 
     75     def popitem(self): # real signature unknown; restored from __doc__
     76         """ 获取并在字典中移除 """
     77         """
     78         D.popitem() -> (k, v), remove and return some (key, value) pair as a
     79         2-tuple; but raise KeyError if D is empty.
     80         """
     81         pass
     82 
     83     def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
     84         """ 如果key不存在,则创建,如果存在,则返回已存在的值且不修改 """
     85         """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
     86         pass
     87 
     88     def update(self, E=None, **F): # known special case of dict.update
     89         """ 更新
     90             {‘name‘:‘alex‘, ‘age‘: 18000}
     91             [(‘name‘,‘sbsbsb‘),]
     92         """
     93         """
     94         D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
     95         If E present and has a .keys() method, does:     for k in E: D[k] = E[k]
     96         If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v
     97         In either case, this is followed by: for k in F: D[k] = F[k]
     98         """
     99         pass
    100 
    101     def values(self): # real signature unknown; restored from __doc__
    102         """ 所有的值 """
    103         """ D.values() -> list of D‘s values """
    104         return []
    105 
    106     def viewitems(self): # real signature unknown; restored from __doc__
    107         """ 所有项,只是将内容保存至view对象中 """
    108         """ D.viewitems() -> a set-like object providing a view on D‘s items """
    109         pass
    110 
    111     def viewkeys(self): # real signature unknown; restored from __doc__
    112         """ D.viewkeys() -> a set-like object providing a view on D‘s keys """
    113         pass
    114 
    115     def viewvalues(self): # real signature unknown; restored from __doc__
    116         """ D.viewvalues() -> an object providing a view on D‘s values """
    117         pass
    118 
    119     def __cmp__(self, y): # real signature unknown; restored from __doc__
    120         """ x.__cmp__(y) <==> cmp(x,y) """
    121         pass
    122 
    123     def __contains__(self, k): # real signature unknown; restored from __doc__
    124         """ D.__contains__(k) -> True if D has a key k, else False """
    125         return False
    126 
    127     def __delitem__(self, y): # real signature unknown; restored from __doc__
    128         """ x.__delitem__(y) <==> del x[y] """
    129         pass
    130 
    131     def __eq__(self, y): # real signature unknown; restored from __doc__
    132         """ x.__eq__(y) <==> x==y """
    133         pass
    134 
    135     def __getattribute__(self, name): # real signature unknown; restored from __doc__
    136         """ x.__getattribute__(‘name‘) <==> x.name """
    137         pass
    138 
    139     def __getitem__(self, y): # real signature unknown; restored from __doc__
    140         """ x.__getitem__(y) <==> x[y] """
    141         pass
    142 
    143     def __ge__(self, y): # real signature unknown; restored from __doc__
    144         """ x.__ge__(y) <==> x>=y """
    145         pass
    146 
    147     def __gt__(self, y): # real signature unknown; restored from __doc__
    148         """ x.__gt__(y) <==> x>y """
    149         pass
    150 
    151     def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
    152         """
    153         dict() -> new empty dictionary
    154         dict(mapping) -> new dictionary initialized from a mapping object‘s
    155             (key, value) pairs
    156         dict(iterable) -> new dictionary initialized as if via:
    157             d = {}
    158             for k, v in iterable:
    159                 d[k] = v
    160         dict(**kwargs) -> new dictionary initialized with the name=value pairs
    161             in the keyword argument list.  For example:  dict(one=1, two=2)
    162         # (copied from class doc)
    163         """
    164         pass
    165 
    166     def __iter__(self): # real signature unknown; restored from __doc__
    167         """ x.__iter__() <==> iter(x) """
    168         pass
    169 
    170     def __len__(self): # real signature unknown; restored from __doc__
    171         """ x.__len__() <==> len(x) """
    172         pass
    173 
    174     def __le__(self, y): # real signature unknown; restored from __doc__
    175         """ x.__le__(y) <==> x<=y """
    176         pass
    177 
    178     def __lt__(self, y): # real signature unknown; restored from __doc__
    179         """ x.__lt__(y) <==> x<y """
    180         pass
    181 
    182     @staticmethod # known case of __new__
    183     def __new__(S, *more): # real signature unknown; restored from __doc__
    184         """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
    185         pass
    186 
    187     def __ne__(self, y): # real signature unknown; restored from __doc__
    188         """ x.__ne__(y) <==> x!=y """
    189         pass
    190 
    191     def __repr__(self): # real signature unknown; restored from __doc__
    192         """ x.__repr__() <==> repr(x) """
    193         pass
    194 
    195     def __setitem__(self, i, y): # real signature unknown; restored from __doc__
    196         """ x.__setitem__(i, y) <==> x[i]=y """
    197         pass
    198 
    199     def __sizeof__(self): # real signature unknown; restored from __doc__
    200         """ D.__sizeof__() -> size of D in memory, in bytes """
    201         pass
    202 
    203     __hash__ = None
    204 
    205 dict
    View Code

2.列表

1.创建列表:

1 name_list = [alex, seven, eric]
2 3 name_list = list([alex, seven, eric])

2.字典的常用操作  

  • 索引
  • 切片
  • 追加
  • 删除
  • 长度
  • 切片
  • 循环
  • 包含
  • 技术分享View Code

3.元组

1.创建元组:

1 ages = (11, 22, 33, 44, 55)
2 3 ages = tuple((11, 22, 33, 44, 55))

2.元组的常用操作  

  • 索引
  • 切片
  • 循环
  • 长度
  • 包含
技术分享
  1 lass tuple(object):
  2     """
  3     tuple() -> empty tuple
  4     tuple(iterable) -> tuple initialized from iterable‘s items
  5     
  6     If the argument is a tuple, the return value is the same object.
  7     """
  8     def count(self, value): # real signature unknown; restored from __doc__
  9         """ T.count(value) -> integer -- return number of occurrences of value """
 10         return 0
 11 
 12     def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
 13         """
 14         T.index(value, [start, [stop]]) -> integer -- return first index of value.
 15         Raises ValueError if the value is not present.
 16         """
 17         return 0
 18 
 19     def __add__(self, y): # real signature unknown; restored from __doc__
 20         """ x.__add__(y) <==> x+y """
 21         pass
 22 
 23     def __contains__(self, y): # real signature unknown; restored from __doc__
 24         """ x.__contains__(y) <==> y in x """
 25         pass
 26 
 27     def __eq__(self, y): # real signature unknown; restored from __doc__
 28         """ x.__eq__(y) <==> x==y """
 29         pass
 30 
 31     def __getattribute__(self, name): # real signature unknown; restored from __doc__
 32         """ x.__getattribute__(‘name‘) <==> x.name """
 33         pass
 34 
 35     def __getitem__(self, y): # real signature unknown; restored from __doc__
 36         """ x.__getitem__(y) <==> x[y] """
 37         pass
 38 
 39     def __getnewargs__(self, *args, **kwargs): # real signature unknown
 40         pass
 41 
 42     def __getslice__(self, i, j): # real signature unknown; restored from __doc__
 43         """
 44         x.__getslice__(i, j) <==> x[i:j]
 45                    
 46                    Use of negative indices is not supported.
 47         """
 48         pass
 49 
 50     def __ge__(self, y): # real signature unknown; restored from __doc__
 51         """ x.__ge__(y) <==> x>=y """
 52         pass
 53 
 54     def __gt__(self, y): # real signature unknown; restored from __doc__
 55         """ x.__gt__(y) <==> x>y """
 56         pass
 57 
 58     def __hash__(self): # real signature unknown; restored from __doc__
 59         """ x.__hash__() <==> hash(x) """
 60         pass
 61 
 62     def __init__(self, seq=()): # known special case of tuple.__init__
 63         """
 64         tuple() -> empty tuple
 65         tuple(iterable) -> tuple initialized from iterable‘s items
 66         
 67         If the argument is a tuple, the return value is the same object.
 68         # (copied from class doc)
 69         """
 70         pass
 71 
 72     def __iter__(self): # real signature unknown; restored from __doc__
 73         """ x.__iter__() <==> iter(x) """
 74         pass
 75 
 76     def __len__(self): # real signature unknown; restored from __doc__
 77         """ x.__len__() <==> len(x) """
 78         pass
 79 
 80     def __le__(self, y): # real signature unknown; restored from __doc__
 81         """ x.__le__(y) <==> x<=y """
 82         pass
 83 
 84     def __lt__(self, y): # real signature unknown; restored from __doc__
 85         """ x.__lt__(y) <==> x<y """
 86         pass
 87 
 88     def __mul__(self, n): # real signature unknown; restored from __doc__
 89         """ x.__mul__(n) <==> x*n """
 90         pass
 91 
 92     @staticmethod # known case of __new__
 93     def __new__(S, *more): # real signature unknown; restored from __doc__
 94         """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
 95         pass
 96 
 97     def __ne__(self, y): # real signature unknown; restored from __doc__
 98         """ x.__ne__(y) <==> x!=y """
 99         pass
100 
101     def __repr__(self): # real signature unknown; restored from __doc__
102         """ x.__repr__() <==> repr(x) """
103         pass
104 
105     def __rmul__(self, n): # real signature unknown; restored from __doc__
106         """ x.__rmul__(n) <==> n*x """
107         pass
108 
109     def __sizeof__(self): # real signature unknown; restored from __doc__
110         """ T.__sizeof__() -- size of T in memory, in bytes """
111         pass
112 
113 tuple
View Code

 

字典丶列表丶元组的一些常用操作