Convert List in python into dictionary

高洛峰
Release: 2017-02-25 13:16:32
Original
1399 people have browsed it

Question 1: How to convert a list into a dictionary?

Problem description: For example, in python, I have the following list, in which the odd positions correspond to the key of the dictionary, and the even positions correspond to value

Convert List in python into dictionary

Solution:

1. Use zip function to implement

Convert List in python into dictionary

2. Use loops to achieve

Convert List in python into dictionary

3. Use enumerate Function generation index to achieve

Convert List in python into dictionary

Question 2 How do we convert two lists into A dictionary?

Problem description: Suppose you have two list

Convert List in python into dictionary

Solution: is still a common zip function

Convert List in python into dictionary

Here we see that the zip function is indeed The pairing has a very good effect. If both lists are large, you need to introduce itertools.izip to solve the problem

Convert List in python into dictionary

Or use the dict function directly below

Convert List in python into dictionary

So what if we have three lsit? For example, we sometimes encounter such problems, such as recording certain data under a longitude and latitude. How to achieve this?

Convert List in python into dictionary

We can see that at this time the zip function can still help us successfully implement the required functions. First, integrate the longitude and latitude one by one. , then connect the vals together, and finally use the dict function to put them together.

Summary

#Through the above example, we know that we can integrate the data through multiple calls of the zip function and finally solve the problem , the above is the entire content of this article, I hope it will be helpful to everyone learning python.

For more articles related to converting List in python into dictionary, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template