Python Dictionary dict Use introduction _python

Source: Internet
Author: User

The creation of a Python dictionary

Method One:

>>> blank_dict = {}
>>> product_dict = {' MAC ': 8000, ' Iphone ': 5000, ' ipad ': 4000, ' MP3 ':
>>> product_dict
{' ipad ': 4000, ' MAC ': 8000, ' Iphone ': 5000, ' mp3 ':
>>> blank_dict, Product_dict
({}, {' ipad ': 4000, ' MAC ': 8000, ' Iphone ': 5000, ' MP3 ': 300})

Method Two:
Starting with the Python 2.2 version

>>> fdict = dict ([' www ', ' www.linuxeye.com '],[' blog ', ' blog.linuxeye.com '])
>>> fdict
{ ' Blog ': ' blog.linuxeye.com ', ' www ': ' www.linuxeye.com '}

Method Three:
Starting with the Python 2.3 release, you can create a "default" dictionary with a handy built-in method Fromkeys () that has the same value for the element in the dictionary (none is default if not given):

>>> fk_dict = {}.fromkeys (' a ', ' B '), ' Linuxeye ')
>>> fk_dict
{' A ': ' Linuxeye ', ' B ': ' Linuxeye '}
>>> fk2_dict = {}.fromkeys (' yeho ', ' python ')
>>> fk2_dict
{' Python ': None , ' Yeho ': None}
>>> fk3_dict = {}.fromkeys (' yeho ', ' python ')
>>> fk3_dict
{' y ': ' Python ' , ' h ': ' Python ', ' e ': ' Python ', ' o ': ' Python '}

Python dictionary check, add, delete, change

>>> product_dict = {' MAC ': 8000, ' iphone ': 5000, ' mp3 ': >>> product_dict[' iphone '] 5000 >> > product_dict[' ipad ' = 4000 #增 >>> product_dict {' ipad ': 4000, ' MAC ': 8000, ' Iphone ': 5000, ' mp3 ': "&G" T;>> Product_dict.keys () #查看索引 [' ipad ', ' MAC ', ' Iphone ', ' mp3 '] >>> product_dict.values () #查看值 [4000, 8000 , 5000,] >>> product_dict.items (' ipad ', 4000), (' MAC ', 8000), (' Iphone ', 5000), (' MP3 ',)] >>&gt ; For Product,price in Product_dict.items (): ... print product,price ... ipad 4000 MAC 8000 Iphone 5000 mp3 ; Product_dict.has_key (' Iphone ') #判断key是否存在 True >>> product_dict.has_key (' Itouch ') False >>> product  _dict.pop (' MAC ') #删除指定key和value 8000 >>> product_dict {' ipad ': 4000, ' Iphone ': 5000, ' mp3 ': +} >>> Product_dict.popitem () #删除第一个key和value (' ipad ', 4000) >>> product_dict {' Iphone ': 5000, ' mp3 ': >&G T;> product_dict = {' IphonE ': 5000, ' mp3 ': >>> del product_dict[' Iphone ' #用del函数删除指定key和value >>> product_dict {' mp3 ': 30 0} >>> product_dict[' mp3 ' = 299 #改 >>> product_dict {' mp3 ': 299} >>> Product_dict.clear () 
 #清空字典内容 (empty dictionary) >>> product_dict {} >>> del product_dict #删除字典 >>> product_dict = {' mp3 ': 300} >>> del product_dict #已删除报错 traceback (most recent call last): File ' <stdin> ', line 1, in &LT;MODULE&G
 T
 Nameerror:name ' product_dict ' is not defined

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.