一.字典
1.字典的定义
user_info={
"name":"alex"
"age":"73"
"gender":"m"
}
2功能
索引
- print(user_info[name])
- print(user_info["age"])
- 无切片:因为索引值不连续
- 循环:默认输出key
- for i in use_info:
- print(i)
- 获取所有的键:print(user_info.keys())
- 获取所有的值:print(user_info.values())
- 获取所有的键值对:print(user_info.items())
- for k v in user_info.items():
- print(k)
- print(v)
- 删除指定键值对
- del user_info("age")
def clear(self): # real signature unknown; restored from __doc__ ------ 清楚所有内容-----
""" D.clear() -> None. Remove all items from D. """ pass def copy(self): # real signature unknown; restored from __doc__ """ D.copy() -> a shallow copy of D """ pass @staticmethod # known case def fromkeys(S, v=None): # real signature unknown; restored from __doc__ """ dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v. v defaults to None. """ pass def get(self, k, d=None): # real signature unknown; restored from __doc__ 根据key获取指定键值,如果不存返回默认值None
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ pass def has_key(self, k): # real signature unknown; restored from __doc__ 检查字典中是否有指定key值
s=user_info.has_key()
s="age" in user_info.key()
""" D.has_key(k) -> True if D has a key k, else False """ return False def items(self): # real signature unknown; restored from __doc__ """ D.items() -> list of D's (key, value) pairs, as 2-tuples """ return [] def iteritems(self): # real signature unknown; restored from __doc__ """ D.iteritems() -> an iterator over the (key, value) items of D """ pass def iterkeys(self): # real signature unknown; restored from __doc__ """ D.iterkeys() -> an iterator over the keys of D """ pass def itervalues(self): # real signature unknown; restored from __doc__ """ D.itervalues() -> an iterator over the values of D """ pass def keys(self): # real signature unknown; restored from __doc__ """ D.keys() -> list of D's keys """ return [] 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 def popitem(self): # real signature unknown; restored from __doc__ ---获取并在字典中移除----
""" D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty. """ pass def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
""" D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """ pass def update(self, E=None, **F): # known special case of dict.update ----批量更新-----
test={
"a1":123,
"a2":456,
}
user_info.update(test)
print(user_info)
""" 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__ """ 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
3.enumerate :为可迭代的对象添加序号
li=[''电脑","鼠标垫","U盘"]
for key item in enumerate(i)
print(key,item)
4.range和xrange指定范围生成指定数字
python2.7 :range会直接打印所有数字,而xrange只会在for循环时打印
python3.5及以上版本,只有range,且range就相当于xrange。
print (xrange(1,10))
for i in range(1,10):
print(i)
输出从大到小的值
for i in range(1,10,-1):
print(i)
输出索引和值
li=['alex','scc']
lee=len(li)
for i in xrange(0,len(li))
print(i,li[i])