您的当前位置:首页正文

字典,python中的map

2024-11-23 来源:个人技术集锦
python中所谓的字典和其他语言中的map其实是一码事
1.基本操作
>>> info={"name":"weihograo","tel":"18665832719",'wife':'huajingfen'}
>>> len(info)
3
>>> info["wife"]
'huajingfen'
>>> "name" in info
True

看看下面和序列的区别
>>> nameArray=["wei","hongrao"]
>>> nameMap={"xing:":"wei","ming":"hongrao"}
>>> nameArray[2]="IT"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> nameMap["work"]="IT"
>>> nameMap
{'work': 'IT', 'ming': 'hongrao', 'xing:': 'wei'}
>>>


2. dict
>>> names=["xing","ming"]
>>> namesvalue=["wei","hongrao"]
>>> namesss=[names,namesvalue]
>>> namesss
[['xing', 'ming'], ['wei', 'hongrao']]
>>> namesssMap=dict(namesss)
>>> namesssMap
{'wei': 'hongrao', 'xing': 'ming'}

3. clear:奇怪的方法
   clear会原地操作,
>>> names={"name":"weihongrao"}
>>> names2=names
>>> names2
{'name': 'weihongrao'}
>>> names={}
>>> names2
{'name': 'weihongrao'}

以上可以看出当names被赋给另个map之后,不会影响到names2的map,就是说原来的map依然存在,names2就是指向原来的map, 但是当用了clear之后,就会使原来的map的值改变见如下

>>> names
{'name': 'weihongrao'}
>>> names2=names
>>> names2
{'name': 'weihongrao'}
>>> names.clear()
>>> names
{}
>>> names2
{}

4. copy :浅度copy,deepcopy:深度copy
 浅度复制
>>> names
{'work': ['IT', 'SDUTY', 'DRIVER'], 'name': 'weihongrao'}
>>> names2=names.copy()
>>> names2
{'work': ['IT', 'SDUTY', 'DRIVER'], 'name': 'weihongrao'}
>>> names["name"]="jixianqiuxue"
>>> names["work"].remove('IT')
>>> names
{'work': ['SDUTY', 'DRIVER'], 'name': 'jixianqiuxue'}
>>> names2
{'work': ['SDUTY', 'DRIVER'], 'name': 'weihongrao'}
因为work是一个序列所以names和names2都指向它,但是name却不一样

深度复制
>>> names
{'work': ['SDUTY', 'DRIVER'], 'name': 'jixianqiuxue'}
>>> names2=deepcopy(names)
>>> names['work']+="IT"
>>> names
{'work': ['SDUTY', 'DRIVER', 'I', 'T'], 'name': 'jixianqiuxue'}
>>> names2
{'work': ['SDUTY', 'DRIVER'], 'name': 'jixianqiuxue'}


5. fromkeys
>>> name={}.fromkeys(["name","age"])
>>> name
{'age': None, 'name': None}
>>> name2=dict.fromkeys(["name","age"])
>>> name2
{'age': None, 'name': None}

6.get, 和[index]区别
>>> names
{'work': ['SDUTY', 'DRIVER', 'I', 'T'], 'name': 'jixianqiuxue'}
>>> names.get("age")
>>> print names.get("age")
None
>>> print names['age']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'age'

7. has_key
>>> names
{'work': ['SDUTY', 'DRIVER', 'I', 'T'], 'name': 'jixianqiuxue'}
>>> names.has_key('age')
False

8. items ,keys
>>> names
{'work': ['SDUTY', 'DRIVER', 'I', 'T'], 'name': 'jixianqiuxue'}
>>> names.items()
[('work', ['SDUTY', 'DRIVER', 'I', 'T']), ('name', 'jixianqiuxue')]
>>> names.keys()
['work', 'name']

9. pop 和popitem
>>> names
{'home': 'guangxi', 'age': '27', 'work': ['SDUTY', 'DRIVER', 'I', 'T'], 'name': 'jixianqiuxue'}
>>> names.pop("home")
'guangxi'
>>> name.popitem()
('age', None)

10. update, 更新和加入
>>> names
{'age': '27', 'work': ['SDUTY', 'DRIVER', 'I', 'T'], 'name': 'jixianqiuxue'}
>>> home={"home":"guangxi"}
>>> names.update(home)
>>> names
{'home': 'guangxi', 'age': '27', 'work': ['SDUTY', 'DRIVER', 'I', 'T'], 'name': 'jixianqiuxue'}
>>> home={"home":"guigang"}
>>> names.update(home)
>>> names
{'home': 'guigang', 'age': '27', 'work': ['SDUTY', 'DRIVER', 'I', 'T'], 'name': 'jixianqiuxue'}

11.values

>>> names
{'home': 'guigang', 'age': '27', 'work': ['SDUTY', 'DRIVER', 'I', 'T'], 'name': 'jixianqiuxue'}
>>> names.values()
['guigang', '27', ['SDUTY', 'DRIVER', 'I', 'T'], 'jixianqiuxue']
>>>
显示全文