Data structures and algorithms-decomposing sequences into separate variables

Source: Internet
Author: User

① problem: If there is a tuple or sequence containing n multiple elements, now you want to break it down into n separate variables.

1 L = (4, 5)2 x, y = l
View Code

② Advanced article:

data = ['sb' ' big hammer ', (2018, 6, 2  = dataa='sb'b=' big hammer '  C=51Date= (2018, 6, 2)

data = ['sb' ' big hammer ', (2018, 6, 2  == 2018= 6= 2
View Code

So then the question comes, this is to know the list of how much of the situation to achieve, if the unknown? that will use the * expression to decompose the iterated object.

* syntax is useful when iterating over a tuple sequence of an edge length, with an example:

Records = [    ('Foo', 1, 2),    ('Bar','Hi'),    ('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)
View Code

Data structures and algorithms-decomposing sequences into separate 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.