Python cookbook (data structure and algorithm) is used to break down elements from any length of iteratable objects.

Source: Internet
Author: User

Python cookbook (data structure and algorithm) is used to break down elements from any length of iteratable objects.

This example describes how to use python to break down elements from iteratable objects of any length. We will share this with you for your reference. The details are as follows:

N elements are extracted from an Iterated object, but the length of the iterated object may exceed N, resulting in an exception of "too many decomposition values;

Use the "* expression" to solve the problem:

Python 3.4.3 (v3.4.3: 9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license () "for more information. >>> * headdata, current = [,] # * the modified variables are located in the first position of the list. You can easily separate headers and tails. >>> headdata [10, 3, 2, 6, 8] >>> current5 >>> record = ('Dave ', 'Dave @ example.com', '2017-555-1212 ', '2017-555-1212') >>> name, email, * phone_numbers = record # * the modified variable is located at the end of the list> name 'Dave '> email 'Dave @ example.com'> phone_numbers ['2017-555-1212', '2017-555-1212 '] >>> line = 'Nobody: *:-2:-3: Unpriviledged User:/var/empty: /usr/bin/false' >>> uname, * fields, homedir, sh = line. split (':') # * the variable is located in the center, combined with string processing operations> uname 'nobody'> homedir '/var/empty'> Sh'/usr/bin/false'> fields ['*', '-2','-3', 'unpriviledged user'] >>> record = ['Lucy ', 50,123.4, (2016,)] >>> name, * _, (* _, year) = record # discard some decomposed values >>> name 'Lucy '>>> year2016 >>>

* Usage is particularly useful in iteration of a variable-length tuples sequence:

(The code is from Python Cookbook.)

Related Article

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.