"Python Cookbook" "Data Structure and algorithm" 1 breaks a sequence into separate variables

Source: Internet
Author: User

If the object is an iterative (any sequence), it can be decomposed, including tuples, lists, strings, files, iterators, and generators, which can be decomposed into separate variables by a simple assignment operation.

The only requirement: The total number of variables matches the sequence, otherwise an error will occur;

Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5, 20:32:19) [MSC v.1500 32bit (Intel)] on Win32type"Copyright","credits" or "license ()"  forMore information.>>> p=[4,5]>>>p[4, 5]>>> x,y=P>>>x4>>>y5>>> data=['Lucy', 50, 12.1, (2016,07,31)]>>>data['Lucy', 50, 12.1, (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'>>> 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 certain values, you can choose a variable name that is not used as the name of the value to discard, such as "_" here .>>>shares50>>> Price12.1>>>

Note: Be sure to discard the value of the selected variable name that has not been used anywhere else.

"Python Cookbook" "Data Structure and algorithm" 1 breaks a sequence into separate variables

Related Article

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.