Python creates a dictionary without too many quotes.

Source: Internet
Author: User

Python creates a dictionary without too many quotes.

This example describes how to create a dictionary without too much reference in python. Share it with you for your reference. The specific implementation method is as follows:

1. Use the itertools Module

import itertoolsthe_key = ['ab','22',33]the_vale = ['aaaa',"dddddddd",'22222222222']d = dict(itertools.izip(the_key,the_vale))print d

2. Add Parameters

dict = dict(red = 1,bule = 2,yellow = 3)print dict

Result: {'yellow': 3, 'bucket': 2, 'red': 1}

3. Use built-in zip Functions
Zip ([iterable,...]) returns a list,

the_key = ['ab','22',33]the_vale = ['aaaa',"dddddddd",'22222222222']dict2 = dict(zip(the_key,the_vale))print type(zip(the_key,the_vale))print dict2

Result:

<type 'list'>{33: '22222222222', 'ab': 'aaaa', '22': 'dddddddd'}

4. fromkeys function of dict
Each created key has the same value.

fromkeys(seq[,value])Create a new dictionary with keys from seq and values set to value.the_key = ['ab','22',33]the_vale = 0d = dict.fromkeys(the_key,the_vale)print 

Result: {33: 0, 'AB': 0, '22': 0}

import stringcount_by_letter = dict.fromkeys(string.ascii_lowercase,0)print count_by_letter

Result:

{'a': 0, 'c': 0, 'b': 0, 'e': 0, 'd': 0, 'g': 0, 'f': 0, 'i': 0, 'h': 0, 'k': 0, 'j': 0, 'm': 0, 'l': 0, 'o': 0, 'n': 0, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0, 't': 0, 'w': 0, 'v': 0, 'y': 0, 'x': 0, 'z': 0}

I hope this article will help you learn Python programming.


How to Create a dictionary in a python dictionary

Create temporary variables
Tmp = dict ()
Tmp. setdefault ('b1 ', 'abc ')
AB ['a3 '] = tmp

Which dictionary method can connect two dictionaries together python

There is an update () method, but if the key is the same, the latter's key-value will overwrite the former.

D. update (E, ** F)-> None. Update D from dict/iterable E and F.
If E has a. keys () method, does: for k in E: D [k] = E [k]
If E lacks. keys () method, does: for (k, v) in E: D [k] = v
In either case, this is followed by: for k in F: D [k] = F [k]

Example:
>>> A = {3: 5, 4: 6}
>>> B = {4: 8, 7: 9}
>>> A. update (B)
>>>
{3: 5, 4: 8, 7: 9}

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.