Python tips: Using * unpacking and itertools.product () to find Cartesian product

Source: Internet
Author: User

Leetcode on the tips when you see a high man use this method to solve problems

"Problem" currently has a string s = "[' A ', ' B '],[' C ', ' d ']", want to separate it into two lists:
List1 = ['a'b']list2 = ['c'D']       
After using Itertools.product () to find Cartesian product, it should be written as:
in Itertools.product (List1, List2):2     print i  
The result is:
(‘a ",  '  C " '  A ",  ' d ' ) ( ' b" c  " '  B ",  ' d")                
However, using eval (s) gets a tuple. Product parameters, if it is a tuple, will be an error (product parameter is two lists, the number of elements in each list is variable). How did it break? "Answer" is actually only one *. * is a technique of assignment in Python, called Unpacking. Believe that many people have seen the Def func (*args, **kwargs) This way of writing, in the function, * represents an indefinite number of parameters, in the form of a tuple, * * is dict. When using a function, it is possible to have a similar method when invoking the Func (*args) function, which is equivalent to taking a tuple of args apart as a parameter into the function. Just do it. It is important to be careful that the number and type of elements contained in args must be consistent with the function definition, otherwise the SYNTAXERROR:INVALID syntax syntax error will be reported. For example, in this problem, it can be written as:
In Itertools.product (*eval (s)):2     print i  
Can come out of the results. Three tips for this problem: (1)itertools.product () asks Cartesian product. Itertools This module has quite a lot of brilliant mathematical algorithms, such as the full array function permutations, the combination function combinations and so on, sometimes want a math class function and do not want to write their own, you can find here, there may be surprises. (2)eval () string evaluation. Eval and exec, the inverse of the two Python functions, are powerful enough to make people less confident about their security. (3)* Unpacking. Already explained above, actually uses the occasion to feel quite limited, has the impression can, can walk in no way when the inspiration is good, do not expect it to give you the program to bring much benefit. ============================= Split Line ========================experiment, the effect of adding * is to let the elements inside the list as parameters, not to add a single list is a parameter
List1 = ['AC','b'] forIinchitertools.product (list1):Print(i)>>>('AC',) ('b',) List1= ['AC','b'] forIinchItertools.product (*list1):Print(i)>>>('a','b')('C','b') List1= [['a','b'],' A'] forIinchItertools.product (*list1):Print(i)>>>('a','1')('a','2')('b','1')('b','2')

Python tips: Using * unpacking and itertools.product () to find Cartesian product (turn)

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.