Application example Analysis of the ZIP () method in Python

Source: Internet
Author: User
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.

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