this article gives an example of the method that Python Cookbook (data structure and algorithm) decomposes the sequence into a single variable. Share to you for your reference, as follows:

if the object is iterative (any sequence), can decompose operations, including tuples, lists, strings, files, iterators and generators, through a simple assignment into single variable.

唯一要求:变量的总数和序列相吻合,否则将出错;

 Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> p=[4,5] >>> p [4, 5] >>> x,y=p >>> x 4 >>> y 5 >>> data=['lucy',50,12.1,(2016,07,31)] >>> data ['lucy', 50, 12.1, (2016, 7, 31)] >>> name,shares,price,date=data >>> name 'lucy' >>> shares 50 >>> price 12.1 >>> date (2016, 7, 31) >>> name,shares,price,(year,mon,day)=data >>> name 'lucy' >>> shares 50 >& Gt; > price 12.1 > > > year 2016 > > > mon 7 > > > day 31 > > > s='hello'> > > s'hello' > > > a, B, C, D, e=s, > > > C'l'> > > D'l' > > > e'o'> > > data=['lucy', 50,12.1, (2016,07,31)] > > > _, shares, price, _=data # when you want to discard some value, you can choose a less than the variable as to the name of the value discarded, so "_" than > & gt; > shares 50; > > > price 12.1 > > > 

note: please make sure that the discarded value choose variable names in other places not used .

("Python Cookbook")

more Python on the relevant content of interested readers can see the special station: "Python data structure and algorithm tutorial " and "Python ", summarizes the skills in the use of function "Python " string manipulation skills summary, "Python entry and advanced tutorial classic " and "Python

operation skills summary" hope for all the Python programming help.

you might be interested in this article:

realization method


This concludes the body part

This paper fixed link:http://www.script-home.com/python-cookbook-data-structure-and-algorithm-method-to-decompose-sequences-into-separate-variables.html | Script Home | +Copy Link

Article reprint please specify:Python Cookbook (data structure and algorithm) method to decompose sequences into separate variables | Script Home

You may also be interested in these articles!