Python converts a list to a dictionary data structure.

Source: Internet
Author: User
Tags bitwise operators python list

Python converts a list to a dictionary data structure.

This example describes how to convert a list in Python to a dictionary data structure. We will share this with you for your reference. The details are as follows:

''' [{'Sympost': 101, 'sort ': 1, 'name': 'aaa'}, {'sympost': 102, 'sort': 2, 'name': 'bbbb'}, {'sympost': 103, 'Port': 3, 'name': 'cccc'}, {'sympost': 104, 'sort ': 4, 'name': 'ddddd'}, {'symbol': 105, 'sort ': 5, 'name': 'eee '}, {'sympost': 106, 'sort ': 6, 'name': 'ffffff'}, {'sympost': 107, 'sort': 7, 'name ': 'gggggg'}, {'symbol': 108, 'sort ': 8, 'name': 'hhhh'}, {'symbol': 109, 'sport': 9, 'name': 'iii'}] is converted to the following structure to obtain the record value {101: {'sort ': 1, 'symbol': 101, 'name': 'aaa'}, 102: {'sort ': 2, 'symbol': 102, 'name': 'bbbb'}, 103: {'sort ': 3, 'symbol': 103, 'name': 'cccccc'}, 104: {'sort ': 4, 'symbol': 104, 'name ': 'dddddd'}, 105: {'sort ': 5, 'symbol': 105, 'name': 'eee'}, 106: {'sort': 6, 'symbol': 106, 'name': 'ffffff'}, 107: {'sort ': 7, 'symbol': 107, 'name': 'gggggg '}, 108: {'sort ': 8, 'symbol': 108, 'name': 'hhhh'}, 109: {'sort': 9, 'symbol': 109, 'name': 'iii'} ''' students = [{'sympost': 101, 'sort ': 1, 'name': 'aaa '}, {'symbol': 102, 'sport': 2, 'name': 'bbbbb'}, {'symbol': 103, 'sort ': 3, 'name ': 'cccccc'}, {'symbol': 104, 'sort ': 4, 'name': 'ddddd'}, {'symbol': 105, 'sport': 5, 'name': 'eeeee'}, {'sympost': 106, 'Port': 6, 'name': 'ffff'}, {'sympost': 107, 'sort ': 7, 'name': 'gggggg'}, {'symbol': 108, 'sort': 8, 'name': 'hhhh '}, {'symbol': 109, 'sort ': 9, 'name': 'iii'}] 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, 'name ': 'aaa' }#}## student = [# {101: {'symbol': 101, 'sort ': 1, 'name': 'aaa' }}#] #

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 = [<Symbol: Symbol object>, <Symbol: Symbol object>, <Symbol: Symbol object>] sorted(symbol_arr, key=lambda s:s.amount, reverse=True)

I hope this article will help you with Python programming.

Articles you may be interested in:
  • Python generator expression and list Parsing
  • Reverse, sort, and sorted
  • Example of how to sort tuples and lists by conditions in Python
  • A preliminary understanding of list and bitwise operators in Python
  • Differences between list generators and generators in Python
  • Differences between extend and append in python list operations
  • How to append an element to a list in python
  • Comparison of map and list derivation efficiency in Python
  • Python calls function instances using the ancestor and list through apply
  • An example is provided to illustrate the usage of the list Data Structure in Python.

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.