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