"Python Cookbook" "Data Structure and algorithm" 2 explode elements from an arbitrary length of an iterative object

Source: Internet
Author: User
Tags explode ming

The n elements are decomposed from an iterative object, but the length of an iterative object may exceed N, and an exception of "too many decomposition values" appears.

Use "* expression" to resolve the problem:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb, 22:43:06) [MSC v.1600 32bit (Intel)] on Win32type"Copyright","credits" or "license ()"  forMore information.>>> *headdata,current=[10,3,2,6,8,5]#* Modified variables are located in the first position of the list, easy to separate head and tail>>>headdata[10, 3, 2, 6, 8]>>> Current5>>> record= ('Xiao Ming','[email protected]','13288888888','13566666666')>>> Name,email,*phone_numbers=record#* The modified variable is in the last position of the list>>>name'Xiao Ming'>>>Email'[email protected]'>>>phone_numbers['13288888888','13566666666']>>> line='nobody:*:-2:-3:unpriviledged User:/var/empty:/usr/bin/false'>>> Uname,*fields,homedir,sh=line.split (':')#* The modified variable is in the middle position and is combined with the string manipulation operation>>>uname'Nobody'>>>Homedir'/var/empty'>>>SH'/usr/bin/false'>>>fields['*','-2','-3','unpriviledged User']>>> record=['Lucy', 50,123.4, (12,13,2016)]>>> name,*_, (*_,year) =record#discard some of the decomposed values>>>name'Lucy'>>> Year2016>>>

* Style usage is especially useful when iterating over a variable-length tuple sequence:

"Python Cookbook" "Data Structure and algorithm" 2 explode elements from an arbitrary length of an iterative object

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.