Python cookbook (data structure and algorithm) is used to break down elements from any length of iteratable objects.
This example describes how to use python to break down elements from iteratable objects of any length. We will share this with you for your reference. The details are as follows:
N elements are extracted from an Iterated object, but the length of the iterated object may exceed N, resulting in an exception of "too many decomposition values;
Use the "* expression" to solve the problem:
Python 3.4.3 (v3.4.3: 9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license () "for more information. >>> * headdata, current = [,] # * the modified variables are located in the first position of the list. You can easily separate headers and tails. >>> headdata [10, 3, 2, 6, 8] >>> current5 >>> record = ('Dave ', 'Dave @ example.com', '2017-555-1212 ', '2017-555-1212') >>> name, email, * phone_numbers = record # * the modified variable is located at the end of the list> name 'Dave '> email 'Dave @ example.com'> phone_numbers ['2017-555-1212', '2017-555-1212 '] >>> line = 'Nobody: *:-2:-3: Unpriviledged User:/var/empty: /usr/bin/false' >>> uname, * fields, homedir, sh = line. split (':') # * the variable is located in the center, combined with string processing operations> uname 'nobody'> homedir '/var/empty'> Sh'/usr/bin/false'> fields ['*', '-2','-3', 'unpriviledged user'] >>> record = ['Lucy ', 50,123.4, (2016,)] >>> name, * _, (* _, year) = record # discard some decomposed values >>> name 'Lucy '>>> year2016 >>>
* Usage is particularly useful in iteration of a variable-length tuples sequence:
(The code is from Python Cookbook.)