python--Dictionary

Source: Internet
Author: User

#字典的速度很快, the more the list element takes more slowly
names={' Nicole ': [18, ' female ', 9999, ' Shanxi ']
,
' haha ': {' age ': +, ' sex ': ' Female '}
}
#查询
#print (names[' QQ ') #如果key不存在会报错
#print (Names.get (' nn ')) #如果key不存在, return None
#增, modify
# names[' Smile ']=[20, ' male ', ' Haidian District '] #如果已经存在则是修改
# Print (names)
#删除
# del names[' haha '] #如果不存在则报错
# Print (names)
# Names.pop (' Nicole ') #如果不存在则报错, Python is officially recommended to use this method
# Names.popitem () #随机删除一个key

#内置方法
Print (Names.keys ()) #返回这个字典所有key的值
Print (Names.values ()) #返回这个字典所有value的值
Names.setdefault (' hehe ', [' Female ', 28,999]) #增加值, if this key of set exists, then the original value is not moved, if it does not exist, it will be added
newnames={' Look ': [18, ' female ', 9999, ' Shanxi ']
,
' Slowly ': {' age ': ' Sex ': ' Female '}
}
Names.update ((newnames)) #扩展字典, update newnames to names
#names. Has_key (' key ') #判断有没有某个key值, there is this method in Python2, Python3 does not have
Print (' hehe ' in names) #判断字典中有没有这个key
# for k in Names:
# print (k) #打印key值
Names.items () #把字典转成一个list
# for K, V in Names.items ():
# print (k,v) #获取字典中key和value对应的值
#字典转换成list之后循环效率会变低, high-efficiency methods can
For k in Names: #这种方式很高效
Print (K,names[k]) #获取字典中key和value对应的值

python--Dictionary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.