Python Learning-Unzip an iterative object to assign values to multiple variables

Source: Internet
Author: User

Extract sequence assignment to multiple objects

Any sequence (or an iterative object) can be extracted and assigned to multiple variables by a simple assignment statement. The only prerequisite is that the number of variables must be the same as the number of elements in the sequence.

>>> data = [' ABCD ', ' a ', 1, 2, (3,4)]
>>> a,b,c,d,e = Data
>>> a,b,c,d,e
(' ABCD ', ' a ', 1, 2, (3, 4))
>>> a,b,c,d, (e,f) = data
>>> F
4
>>>

This decompression assignment can be used on any iterator object, not just a list or a meta-ancestor. Includes strings, file objects, iterators, and generators.

>>> s= ' Hello '
>>> A,b,c,d,e = s
>>> E
' O '
>>>

Sometimes, you might just want to unzip a part and discard other values. For this scenario, Python does not provide a special syntax. But you can use any variable name to take a place, and then lose these variables.

But you have to make sure that the placeholder names you choose are not used anywhere else.

* Use of an asterisk expression

If you want to count the average score of 24 exams for a course, you need to exclude the first and last scores, you can use the asterisk expression.

def Grades_avg (Grades):    fist,*middle,last = Grades    return sum (middle)/len (middle)

In another case, suppose you now have a list of users ' records, each containing a name, a message, and then an indeterminate number of phone numbers. You can break down these records as follows:

>>> record = ('Dave','[email protected]','773-555-1212','847-555-1212')>>> name,email,*phone_numbers =Record>>>name'Dave'>>>Email'[email protected]'>>>phone_numbers['773-555-1212','847-555-1212']>>>

An asterisk expression can also be used at the beginning of an iterative object:

>>> a = (' Wang ', ' Li ', ' Zhang ', ' Male ', 30)
>>> *name,sex,age = A
>>> Name
[' Wang ', ' Li ', ' Zhang ']
>>>

Attention:

The variables extracted from the asterisk expression above are list types, regardless of the type of object being decompressed, so that you can use the asterisk expression to extract the object without having to check it to what type is low (always list)

It is important to note that an asterisk expression is useful when iterating over a sequence of elements that are variable-length tuples. Like what

Records = [('Foo', 1, 2),('Bar','Hello'),('Foo', 3, 4),]defDo_foo (x, y):Print('Foo', x, y)defDo_bar (s):Print('Bar', s) forTag,*argsinchRecords:ifTag = ='Foo': Do_foo (*args)elifTag = ='Bar': Do_bar (*args) output: Foo1 2Bar Hellofoo3 4

The asterisk decompression syntax can also be useful for string manipulation, such as the segmentation of strings:

>>> line = ' nobody:*:-2:-2:unprivileged user:/var/empty:/usr/bin/false '
>>> uname,*fields,homedir,sh = Line.split (': ')
>>> uname
' Nobody '
>>> Homedir
'/var/empty '
>>> SH
'/usr/bin/false '
>>>

Python Learning-Unzip an iterative object to assign values to multiple variables

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.