This paper analyzes the application of the ZIP () method in Python. Share to everyone for your reference, as follows:
Suppose you have a set set, you need to specify a unique ID for each element in the set, so that the group is built into a dict structure.
This scenario can evolve into two list/set or a set with a list of how to create a dictionary, such as:
A = ["A", "B", "C", "D"]b = [1, 2, 3, 4]? ==>c = {"A": 1, "B": 2, "C": 3, "D": 4}
A quick way is to use the built-in method Zip () to implement. Examples are as follows:
>>> a = ["A", "B", "C", "D"]>>> b = [1, 2, 3, 4]>>>>>> c = dict (Zip (A, b)) >>> C{' A ': 1, ' C ': 3, ' B ': 2, ' d ': 4}>>> E = range (0, Len (a)) >>> F = dict (Zip (A, E)) >>> f{' a ': 0, ' C ': 2, ' B ': 1, ' d ': 3}
Zip,dict are all build-in methods, and their efficiency is very high. For a list of length millions, it takes only 10 seconds (but of course it has to do with the performance of the machine itself:-))
More readers interested in Python related content can view this site topic: "Python function Tips Summary", "Python string manipulation Tips", "Python Introductory and Advanced Classic tutorial" and "Python file and directory Operations Tips Summary"
I hope this article is helpful for Python program design.