Dictionary application instance in python and python dictionary application instance

Source: Internet
Author: User

Dictionary application instance in python and python dictionary application instance

The keys in the dictionary must meet the following two conditions:

1. Each key can correspond to only one item, that is, it is not allowed when one key corresponds to multiple values (except list, tuples, and other dictionary container objects ). When a key conflict occurs (that is, the dictionary key is assigned a value repeatedly), the last value is assigned.

>>> myuniversity_dict = {'name':'yuanyuan', 'age':18, 'age':19, 'age':20, 'schoolname':Chengdu, 'schoolname':Xinxiang}Traceback (most recent call last):  File "<stdin>", line 1, in <module>NameError: name 'Chengdu' is not defined>>> myuniversity_dict = {'name':'yuanyuan', 'age':18, 'age':19, 'age':20, 'schoolname':'Chengdu', 'schoolname':'Xinxiang'}>>> myuniversity_dict{'age': 20, 'name': 'yuanyuan', 'schoolname': 'Xinxiang'}>>> 

2. Keys must be Hash-able, and can be variable types such as lists and dictionaries. They cannot be used as Dictionary keys because they cannot be Hash-able.

Why? -- The Interpreter calls the hash function to calculate the location of your data based on the value in the dictionary. If the key is a mutable object, you can modify the key itself. When the key changes, the hash function maps to different addresses to store data, in this way, the hash function cannot store or obtain relevant data reliably. The reason for selecting a hash key is that their values cannot be changed. An example of extracting python core programming (version 2) is as follows:

#!/usr/bin/env pythondb = {}def newuser():    prompt = 'login desired: '    while True:        name = raw_input(prompt)        if db.has_key(name):            prompt = 'name taken, try another\n'            continue        else:            break    pwd = raw_input('passwd: ')    db[name] = pwddef olduser():    name = raw_input('login: ')    pwd = raw_input('passwd: ')    passwd = db.get(name)    if passwd == pwd:       print 'welcome back', name    else:        print 'login incorrect'def showmenu():    prompt = """(N)ew User Login(E)xisting User Login(Q)uitEnter choice:"""    done = False    while not done:        chosen = False        while not chosen:            try:               choice = raw_input(prompt).strip()[0].lower()            except:               choice = 'q'            print '\nYou picked: [%s]' % choice            if choice not in 'neq':               print 'invalid option, try again'            else:               chosen = True        if choice == 'q':done = True        if choice == 'n':newuser()        if choice == 'e':olduser()if __name__ == '__main__':    showmenu()

 

Running result:

[root@localhost src]# python usrpw.py (N)ew User Login(E)xisting User Login(Q)uitEnter choice:nYou picked: [n]login desired: rootpasswd: 1(N)ew User Login(E)xisting User Login(Q)uitEnter choice:nYou picked: [n]login desired: rootname taken, try another

 

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.