A method of translating a Python implementation list into a dictionary data structure

Source: Internet
Author: User
The examples in this paper describe the method of translating the Python implementation list into a dictionary data structure. Share to everyone for your reference, as follows:

' [{' symbol ': 101, ' sort ': 1, ' name ': ' AAAA '}, {' symbol ': 102, ' sort ': 2, ' name ': ' bbbb '}, {' symbol ': 103, ' sort ': 3, ' Name ': ' CCCC '}, {' symbol ': 104, ' sort ': 4, ' name ': ' dddd '}, {' symbol ': ' + ', ' sort ': 5, ' name ': ' Eeee '}, {' symbol ': 10  6, ' sort ': 6, ' name ': ' FFFF '}, {' symbol ': 107, ' sort ': 7, ' name ': ' GGGG '}, {' symbol ': 108, ' sort ': 8, ' name ': ' HHhH '}, {' symbol ': 109, ' sort ': 9, ' name ': ' IIII '}]  Converted to the following structure, it is easy to get the record value by ID {101: {' sort ': 1, ' symbol ': ' 101, ' name ': ' AAAA '}, 102: {' sort ': 2, ' symbol ': 102, ' name ': ' BBBB '}, 103: {' sort ': 3, ' symbol ': ' 103 ', ' name ': ' CCCC '}, 104: {' sort ': 4, ' symbol ': ' 104 ', ' name ': ' dddd '}, $: {' sort ': 5, ' sym Bol ':, ' name ': ' Eeee '}, 106: {' sort ': 6, ' symbol ': 106, ' name ': ' FFFF '}, 107: {' sort ': 7, ' symbol ': 107, ' name ': ' GG    GG '}, 108: {' sort ': 8, ' symbol ': 108, ' name ': ' HHhH '}, 109: {' sort ': 9, ' symbol ': 109, ' name ': ' IIII '} ' students = [ {' symbol ': 101, ' sort ': 1, ' name ': ' AAAA '}, {' symbol ': 102, ' sort ': 2, ' name ': ' bbbb '}, {' SymboL ': 103, ' sort ': 3, ' name ': ' CCCC '}, {' symbol ': 104, ' sort ': 4, ' name ': ' dddd '}, {' symbol ': ' + ', ' sort ': 5, ' name ': ' Eeee '}, {' symbol ': 106, ' sort ': 6, ' name ': ' FFFF '}, {' symbol ': 107, ' sort ': 7, ' name ': ' GGGG '}, {' symbol ': 108, ' Sort ': 8, ' name ': ' HHhH '}, {' symbol ': 109, ' sort ': 9, ' name ': ' IIII '}]symbol_list = map (lambda x: (x.get (' symbol '), x), students) print symbol_listprint '-----------------------------------------' Print dict (symbol_list) #value_list = Map (lambda x:x, students) # # #print dict (Zip (symbol_list, value_list)) # # #student = {# 101: {' symbol ': 101, ' sort ': 1, ' n Ame ': ' AAAA '}#}# #student = [# {101: {' symbol ': 101, ' sort ': 1, ' name ': ' AAAA '}}#]#

Sorting usage:

Students = [      {' name ': ' Zhangsan ', ' price ': 20.01, ' date ': ' 2015-01-09t01:00:00z '},      {' name ': ' Lisi ', ' price ' : 10.01, ' date ': ' 2013-01-09t01:00:00z '},      {' name ': ' Wangwu ', ' price ': 0.01, ' date ': ' 2012-01-09t01:00:00z '}] Print Sorted (students, KEY=LAMBDA s:s["name"]) print sorted (students, KEY=LAMBDA s:s["Price"], reverse=true) print sorted ( Students, Key=lambda s:s["date"], reverse=true) AA = sorted (students, KEY=LAMBDA s:s["Price"], reverse=true) sorted (AA, Key=lambda s:s["Date"], reverse=true) Symbol_arr = [
 
  
   
  , 
  
   
    
   , 
   
    
     
    ] Sorted (Symbol_ Arr, Key=lambda S:s.amount, reverse=true)
   
    
  
   
 
  

I hope this article is helpful for Python program design.

  • 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.