Notes (doing things in Python)--variables (dictionaries)

Source: Internet
Author: User
Tags shallow copy

  1. Mode key-value:dict={' xiaoming ': all, ' xiaohaong ':, ' Xiaomao ': $, ' Xiaoli ': 54};
  2. Use the constructor to build the dictionary dict (tuple)/dict (a=1,b=2,c=3), zip to generate a dictionary of two identical lengths of list dict (zip (lista,listb)); The ZIP function accepts any number of sequences (including 0 and 1) as parameters, returning a tuple List. Detailed view: Python's zip function
  3. Common Operation: keys,values,get,update,del,clear, nested;
    Code_dict.update ({'php': 34254})  // plug in new data code_dict.clear ()                //empty Dictionary
  4.  1  code_dict = { " c  : 123,"  shell   ": 3434,"   Java  : 7442 2  a = code_dict.get ( " python  Span style= "color: #800000;" > ' , None)// No python returned none  3  b = Code_dict.get (   ' , ' aaaaa ')//cannot find python returns AAAAA 
  5. Dictionary: is a hash table, no sequential data structure, suitable for insertion, query operation; (space for Speed)
  6. Dictionary: key is not necessarily a string, but it must be an immutable object;
  7. Dictionary: sort [(k,dict[k]) for K in sorted (dict.keys ())]
  8. Dictionary: sorted (dict.iteritems (), key=lambda d:d[1], Reverse=true)
  9. References to Dictionaries:
    1 l=[4,5,6]2 x=l*4      //x=[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]3 y=[l]*4    y=[[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]4 l[1]=0     //l=[4, 0, 6]5PR int X    //x=[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]6 pirnt Y    //y=[[4, 0, 6], [4, 0, 6], [ 4, 0, 6], [4, 0, 6]
  10. A shallow copy of the Dictionary (the Dictionary is nested, only the first layer is copied): dictionary d.copy (), copy.copy (dictionary d)
  11. Deep copy of the dictionary (dictionary with nested, full copy): copy.deepcopy (dictionary d)

Notes (doing things in Python)--variables (dictionaries)

Related Article

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.