博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python字典使用
阅读量:5051 次
发布时间:2019-06-12

本文共 7442 字,大约阅读时间需要 24 分钟。

>>> dic = dict((("A",[1,2,3]),("F",56)))>>> dic{
'A': [1, 2, 3], 'F': 56}>>> dic = {
"R":"RRRR","T":"TTTT"}>>> dic{
'R': 'RRRR', 'T': 'TTTT'}>>> dic["R"]'RRRR'>>> >>> dic = dict((("A",[1,2,3]),("F",56)))>>> dic["A"][1, 2, 3]>>> dic["A"][0]1>>> dic["E"] = "RYFHF">>> dic{
'A': [1, 2, 3], 'F': 56, 'E': 'RYFHF'}>>> dict1 = {}>>> dict1.formkeys((1,2,3),"number")Traceback (most recent call last): File "
", line 1, in
dict1.formkeys((1,2,3),"number")AttributeError: 'dict' object has no attribute 'formkeys'>>> dict1 = dict1.formkeys((1,2,3),"number")Traceback (most recent call last): File "
", line 1, in
dict1 = dict1.formkeys((1,2,3),"number")AttributeError: 'dict' object has no attribute 'formkeys'>>> dict1 = {
"A":"a","B":"b"}>>> d = dict1.keys>>> d
>>> for i in d : print(i) Traceback (most recent call last): File "
", line 1, in
for i in d :TypeError: 'builtin_function_or_method' object is not iterable>>> dict1.get("A")'a'>>> dict1.set("C","c")Traceback (most recent call last): File "
", line 1, in
dict1.set("C","c")AttributeError: 'dict' object has no attribute 'set'>>> dict1.get("C","c")'c'>>> dict1{ 'A': 'a', 'B': 'b'}>>> "A" in dict1True>>> dict1{ 'A': 'a', 'B': 'b'}>>> dict1.clear()>>> dict1{}>>> a = { "x":"XXXX","e":"EEEEEE"}>>> b= a>>> a = {}>>> a{}>>> b{ 'x': 'XXXX', 'e': 'EEEEEE'}>>> b["t"] = "TTTT">>> b{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}>>> for i in b.keysSyntaxError: invalid syntax>>> or i in b.keys: SyntaxError: invalid syntax>>> for i in b.keys: print(i) Traceback (most recent call last): File "
", line 1, in
for i in b.keys:TypeError: 'builtin_function_or_method' object is not iterable>>> for i in b.items(): print(i) ('x', 'XXXX')('e', 'EEEEEE')('t', 'TTTT')>>> for i in b.keys(): print(i) xet>>> for i in b.values(): print(i) XXXXEEEEEETTTT>>> for i in b.items(): if i["x"] == "XXXX": print(i) Traceback (most recent call last): File "
", line 2, in
if i["x"] == "XXXX":TypeError: tuple indices must be integers or slices, not str>>> for i in b.items(): if i["x"] == "XXXX": print("i") Traceback (most recent call last): File "
", line 2, in
if i["x"] == "XXXX":TypeError: tuple indices must be integers or slices, not str>>> b{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}>>> b.get("x")'XXXX'>>> b.get("d","DDDD")'DDDD'>>> b{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}>>> b.set("d","DDDD")Traceback (most recent call last): File "
", line 1, in
b.set("d","DDDD")AttributeError: 'dict' object has no attribute 'set'>>> help(b)Help on dict object: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) | | Methods defined here: | | __contains__(self, key, /) | True if D has a key k, else False. | | __delitem__(self, key, /) | Delete self[key]. | | __eq__(self, value, /) | Return self==value. | | __ge__(self, value, /) | Return self>=value. | | __getattribute__(self, name, /) | Return getattr(self, name). | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __gt__(self, value, /) | Return self>value. | | __init__(self, /, *args, **kwargs) | Initialize self. See help(type(self)) for accurate signature. | | __iter__(self, /) | Implement iter(self). | | __le__(self, value, /) | Return self<=value. | | __len__(self, /) | Return len(self). | | __lt__(self, value, /) | Return self
size of D in memory, in bytes | | clear(...) | D.clear() -> None. Remove all items from D. | | copy(...) | D.copy() -> a shallow copy of D | | fromkeys(iterable, value=None, /) from builtins.type | Returns a new dict with keys from iterable and values equal to value. | | get(...) | D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. | | items(...) | D.items() -> a set-like object providing a view on D's items | | keys(...) | D.keys() -> a set-like object providing a view on D's keys | | 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 | | popitem(...) | D.popitem() -> (k, v), remove and return some (key, value) pair as a | 2-tuple; but raise KeyError if D is empty. | | setdefault(...) | D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D | | update(...) | D.update([E, ]**F) -> None. Update D from dict/iterable E and F. | If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] | If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v | In either case, this is followed by: for k in F: D[k] = F[k] | | values(...) | D.values() -> an object providing a view on D's values | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __hash__ = None>>> b{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}>>> setitem(b,"d","DDD")Traceback (most recent call last): File "
", line 1, in
setitem(b,"d","DDD")NameError: name 'setitem' is not defined>>> for i in b.keys(): if i in b: print(i) break x>>> for i in b.keys(): if i in b: print(i) xet>>> b["y"] = "RRRRR">>> b{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'y': 'RRRRR'}>>> a = b.copy()>>> a{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'y': 'RRRRR'}>>> b.clear()>>> b{}>>> a{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'y': 'RRRRR'}>>> id(a)4387105744>>> id(b)4386400656>>> a.popitem()('y', 'RRRRR')>>> a{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}>>> a.setdifault("b","BBBB")Traceback (most recent call last): File "
", line 1, in
a.setdifault("b","BBBB")AttributeError: 'dict' object has no attribute 'setdifault'>>> a{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT'}>>> a.setdefault("v","VVVV")'VVVV'>>> a{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'v': 'VVVV'}>>> a.get("e")'EEEEEE'>>> a{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'v': 'VVVV'}>>> a.setdefault("n","NNNNN")'NNNNN'>>> a{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}>>> a.updata("g","GGGG")Traceback (most recent call last): File "
", line 1, in
a.updata("g","GGGG")AttributeError: 'dict' object has no attribute 'updata'>>> a{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}>>> a.update("e","update")Traceback (most recent call last): File "
", line 1, in
a.update("e","update")TypeError: update expected at most 1 arguments, got 2>>> a{ 'x': 'XXXX', 'e': 'EEEEEE', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}a>>> a.update({ "e","update"})Traceback (most recent call last): File "
", line 1, in
a.update({ "e","update"})ValueError: dictionary update sequence element #0 has length 1; 2 is required>>> a.update({ "e":"update"})>>> a{ 'x': 'XXXX', 'e': 'update', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}>>> a.update("e":"update2")SyntaxError: invalid syntax>>> a.update({ "e":"update2"})>>> a{ 'x': 'XXXX', 'e': 'update2', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}>>> a.setdefault("e","rrrr")'update2'>>> a{ 'x': 'XXXX', 'e': 'update2', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN'}>>> a.setdefault("e1","XXXX")'XXXX'>>> a{ 'x': 'XXXX', 'e': 'update2', 't': 'TTTT', 'v': 'VVVV', 'n': 'NNNNN', 'e1': 'XXXX'}>>>

 

转载于:https://www.cnblogs.com/zkzzkz/p/6575966.html

你可能感兴趣的文章
mysql 多表管理修改
查看>>
group by order by
查看>>
bzoj 5252: [2018多省省队联测]林克卡特树
查看>>
https 学习笔记三
查看>>
Oracle学习之简单查询
查看>>
log4j配置
查看>>
linux 配置SAN存储-IPSAN
查看>>
双链表
查看>>
java学习笔记之String类
查看>>
pymysql操作mysql
查看>>
Linux服务器删除乱码文件/文件夹的方法
查看>>
牛腩记账本core版本源码
查看>>
Word Break II
查看>>
UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
查看>>
jdk从1.8降到jdk1.7失败
查看>>
一些关于IO流的问题
查看>>
mongo备份操作
查看>>
8 -- 深入使用Spring -- 3...1 Resource实现类InputStreamResource、ByteArrayResource
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
一个关于vue+mysql+express的全栈项目(六)------ 聊天模型的设计
查看>>