Question 1: How to convert a list into a dictionary?
problem Description: For example, in Python I have a list of the following, where the odd position corresponds to the dictionary key
, the even position of the correspondingvalue
Solution:
1. Using the ZIP function to implement
2. Using loops to achieve
3. Using enumerate
function generation index
to achieve
Question 2 How do we convert two lists into a dictionary?
problem Description: Suppose you have twolist
Solution: still a common zip
function
Here we see zip
that the function does a good job on pairing, and if two lists are large, you need itertools.izip
to introduce them to solve the problem.
or the following direct use dict
function
So what if we had three lsit? For example, we sometimes encounter such problems such as in a latitude and longitude to record a certain data, this time how to implement it?
We can see at this time that the zip
function can still help us to achieve the required function successfully, first of all the latitude and longitude one by one pairing together, then the Val together, and finally use dict
the function together.
Summarize
Through the above example, we know that the zip function can be used multiple times to consolidate the data, finally solve the problem, the above is the entire content of this article, I hope to learn Python can help you.