Python cookbook (data structure and algorithm) is a method for decomposing sequences into individual variables.

Source: Internet
Author: User

Python cookbook (data structure and algorithm) is a method for decomposing sequences into individual variables.

This example describes how to use a Python cookbook (data structure and algorithm) to separate sequences into individual variables. We will share this with you for your reference. The details are as follows:

If an object is iteratable (any sequence), it can be decomposed, including tuples, lists, strings, files, iterators, and generators, A simple value assignment operation can be used to separate variables.

Unique requirement: the total number of variables matches the sequence; otherwise, an error occurs;

Python 2.7.11 (v2.7.11: 6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license () "for more information. >>> p = []> p [4, 5]> x, y = p> x4> y5> data = ['Lucy ', 50, 12.1, (12.1, 31)]> data ['Lucy ', 50, 2016, (, 7, 31)]> name, shares, price, date = data >>> name 'Lucy '>>> shares50 >>> price12.1 >>> date (2016, 7, 31) >>>> name, shares, price, (year, mon, day) = data >>> name 'Lucy '>>> shares50 >>>> price12.1 >>> year2016 >>>> mon7 >>> day31 >>> s = 'hello' >>> s 'hello'>, b, c, d, e = s> c 'l'> d 'l'> e 'O'> data = ['Lucy ', 50, 12.1, (, 31)] >>_, shares, price, _ = data # When you want to discard some values, you can select a variable name that is not used as the name of the value to be discarded, for example, "_" >>>> shares50 >>> price12.1 >>>

Note:Make sure that the variable name selected for the dropped value has never been used elsewhere.

(The code is from Python Cookbook.)

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.