Python开发(基础):列表Dict

简介:

Dict 内置函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/user/bin/evn python
# -*- coding:utf-8 -*-
 
userinfo  =  {
     1 : 'alex' ,
     'age' : 19 ,
     3 : 'tony'
}
print  userinfo
# class dict(object):
#     """
#     dict() -> new empty dictionary
#     dict(mapping) -> new dictionary initialized from a mapping object's
#         (key, value) pairs
#     dict(iterable) -> new dictionary initialized as if via:
#         d = {}
#         for k, v in iterable:
#             d[k] = v
#     dict(**kwargs) -> new dictionary initialized with the name=value pairs
#         in the keyword argument list.  For example:  dict(one=1, two=2)
#     清除出所健值对
#     def clear(self): # real signature unknown; restored from __doc__
#         """ D.clear() -> None.  Remove all items from D. """
#         pass
#
# userinfo.clear()
# print userinfo
#     def copy(self): # real signature unknown; restored from __doc__
#         复制,类似于赋值运算符
#         """ D.copy() -> a shallow copy of D """
#         pass
#
# userinfo2 = userinfo.copy()
# print userinfo2
#     @staticmethod # known case
#     def fromkeys(S, v=None): # real signature unknown; restored from __doc__
#         """
#         根据所给可迭代的量,将其他为健,将v作为值新生成一个dict,如果v没给,默认为None
#         dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
#         v defaults to None.
#         """
#         pass
# userinfo2  = userinfo.fromkeys(['age','tall','heavy'])
# print userinfo2
# 结果:{'heavy': None, 'tall': None, 'age': None}
#     def get(self, k, d=None): # real signature unknown; restored from __doc__
#         根据键,获取值
#         """ D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None. """
#         pass
# print userinfo.get(1)
# 结果:alex
#     def has_key(self, k): # real signature unknown; restored from __doc__
#         判断dict中是否有某个键
#         """ D.has_key(k) -> True if D has a key k, else False """
#         return False
#print userinfo.has_key(1)
# 结果:True
#    def items(self): # real signature unknown; restored from __doc__
#         """ D.items() -> list of D's (key, value) pairs, as 2-tuples """
#         return []
#print userinfo.items()
# 结果[(1, 'alex'), ('age', 19), (3, 'tony')]
# for k,v in userinfo.items():
#     print  str(k) +':'+str(v)
# 结果:
# 1:alex
# age:19
# 3:tony
 
#     def iteritems(self): # real signature unknown; restored from __doc__
#         """ D.iteritems() -> an iterator over the (key, value) items of D """
#         pass
# for k,v in userinfo.iteritems():
#     print  str(k) +':'+str(v)
# 结果:
# 1:alex
# age:19
# 3:tony
#     def iterkeys(self): # real signature unknown; restored from __doc__
#         """ D.iterkeys() -> an iterator over the keys of D """
#         pass
# for k in userinfo.iterkeys():
#     print k
# 结果:
# 1
# age
# 3
#     def itervalues(self): # real signature unknown; restored from __doc__
#         """ D.itervalues() -> an iterator over the values of D """
#         pass
# for v in userinfo.itervalues():
#     print v
# 结果:
# alex
# 19
# tony
#     def keys(self): # real signature unknown; restored from __doc__
#         返回dict所有键的list
#         """ D.keys() -> list of D's keys """
#         return []
# print userinfo.keys()
# 结果:[1, 'age', 3]
#     def pop(self, k, d=None): # real signature unknown; restored from __doc__
#         根据所给的健值,来移除某个键值对,如果所给键值不对或为空,则会报错
#         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
#         """
#         pass
# userinfo.pop('age')
# print userinfo
# 结果:{1: 'alex', 3: 'tony'}
#     def popitem(self): # real signature unknown; restored from __doc__
#         每次移除一个键值对,从第个键值对开始移除,如果dict为空,则会报错
#         D.popitem() -> (k, v), remove and return some (key, value) pair as a
#         2-tuple; but raise KeyError if D is empty.
#         """
#         pass
# userinfo.popitem()
# print userinfo
# 结果:{'age': 19, 3: 'tony'}
#     def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
#         所给键如果存在于dict中,则原来dict中的键所对应的值不变,如果不存在,则dict上新增一个键值对,如果所给值为空,则默认为None
#         """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
#         pass
#
# userinfo.setdefault('name',20)
# print userinfo
# 结果:{1: 'alex', 'age': 19, 3: 'tony', 'name': 20}
#     def update(self, E=None, **F): # known special case of dict.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]
#         """
#         pass
#
#     def values(self): # real signature unknown; restored from __doc__
#         返回dict值的list
#         """ D.values() -> list of D's values """
#         return []
#
#     def viewitems(self): # real signature unknown; restored from __doc__
#         """ D.viewitems() -> a set-like object providing a view on D's items """
#         pass
#
#     def viewkeys(self): # real signature unknown; restored from __doc__
#         """ D.viewkeys() -> a set-like object providing a view on D's keys """
#         pass
#
#     def viewvalues(self): # real signature unknown; restored from __doc__
#         """ D.viewvalues() -> an object providing a view on D's values """
#         pass
#
#     def __cmp__(self, y): # real signature unknown; restored from __doc__
#         """ x.__cmp__(y) <==> cmp(x,y) """
#         pass
#
#     def __contains__(self, k): # real signature unknown; restored from __doc__
#         """ D.__contains__(k) -> True if D has a key k, else False """
#         return False
#
#     def __delitem__(self, y): # real signature unknown; restored from __doc__
#         """ x.__delitem__(y) <==> del x[y] """
#         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 __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 __init__(self, seq=None, **kwargs): # known special case of dict.__init__
#         """
#         dict() -> new empty dictionary
#         dict(mapping) -> new dictionary initialized from a mapping object's
#             (key, value) pairs
#         dict(iterable) -> new dictionary initialized as if via:
#             d = {}
#             for k, v in iterable:
#                 d[k] = v
#         dict(**kwargs) -> new dictionary initialized with the name=value pairs
#             in the keyword argument list.  For example:  dict(one=1, two=2)
#         # (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
#
#     @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 __setitem__(self, i, y): # real signature unknown; restored from __doc__
#         """ x.__setitem__(i, y) <==> x[i]=y """
#         pass
#
#     def __sizeof__(self): # real signature unknown; restored from __doc__
#         """ D.__sizeof__() -> size of D in memory, in bytes """
#         pass
#
#     __hash__ = None


本文转自  wbb827  51CTO博客,原文链接:http://blog.51cto.com/wbb827/1934513
相关文章
|
5月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
376 1
|
5月前
|
开发者 Python
Python列表推导式:优雅与效率的完美结合
Python列表推导式:优雅与效率的完美结合
500 116
|
5月前
|
大数据 开发者 Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
441 109
|
5月前
|
Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
512 119
|
5月前
|
Python
Python列表推导式:优雅与效率的艺术
Python列表推导式:优雅与效率的艺术
359 99
|
5月前
|
数据处理 Python
解锁Python列表推导式:优雅与效率的完美融合
解锁Python列表推导式:优雅与效率的完美融合
378 99
|
5月前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
508 95
|
5月前
|
Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
|
5月前
|
索引 Python
Python 列表切片赋值教程:掌握 “移花接木” 式列表修改技巧
本文通过生动的“嫁接”比喻,讲解Python列表切片赋值操作。切片可修改原列表内容,实现头部、尾部或中间元素替换,支持不等长赋值,灵活实现列表结构更新。
246 1
|
5月前
|
索引 Python
098-python列表_切片_slice_开始_结束
本文介绍了Python中列表的切片(slice)操作,通过“前闭后开”原则截取列表片段,支持正负索引、省略端点等用法,并结合生活实例(如切面包、直播切片)帮助理解。切片不改变原列表,返回新列表。
371 4

推荐镜像

更多