标签:Combination Python permutations list print length Permutation combinations input
First import itertools package to implement permutations method in python. This method takes a list as an input and return an object list of tuples that contain all permutation in a list form.
# A Python program to print all # permutations using library function from itertools import permutations # Get all permutations of [1, 2, 3] perm = permutations([1, 2, 3]) # Print the obtained permutations for i in list(perm): print i
Output
(1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)
It generates n! permutations if length of input sequence is n.
If you want to get permutations of length L then implement it in this way.
# A Python program to print all # permutations of given length from itertools import permutations # Get all permutations of length 2 # and length 2 perm = permutations([1, 2, 3], 2) # Print the obtained permutations for i in list(perm): print i
Output
(1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2)
It generate nCr * r! permutations if length of input sequence is n and input parameter is r.
This method takes a list and a input r as a input and return a object list of tuples which contain all possible combination of length r in a list form.
# A Python program to print all # combinations of given length from itertools import combinations # Get all combinations of [1, 2, 3] # and length 2 comb = combinations([1, 2, 3], 2) # Print the obtained combinations for i in list(comb): print i
Output
(1, 2) (1, 3) (2, 3)
1.Combinations are emitted in lexicographic sort order of input. So, if the input list is sorted, the combination tuples will be produced in sorted order.
# A Python program to print all combinations # of given length with unsorted input. from itertools import combinations # Get all combinations of [2, 1, 3] # and length 2 comb = combinations([2, 1, 3], 2) # Print the obtained combinations for i in list(comb): print i
Output
(2, 1) (2, 3) (1, 3)
2.Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.
# A Python program to print all combinations # of given length with duplicates in input from itertools import combinations # Get all combinations of [1, 1, 3] # and length 2 comb = combinations([1, 1, 3], 2) # Print the obtained combinations for i in list(comb): print i
Output
(1, 1) (1, 3) (1, 3)
3.If we want to make combination of same element to same element then we use combinations_with_replacement.
# A Python program to print all combinations # with an element-to-itself combination is # also included from itertools import combinations_with_replacement # Get all combinations of [1, 2, 3] and length 2 comb = combinations_with_replacement([1, 2, 3], 2) # Print the obtained combinations for i in list(comb): print i
Output
(1, 1) (1, 2) (1, 3) (2, 2) (2, 3) (3, 3)
https://www.geeksforgeeks.org/permutation-and-combination-in-python/
标签:Combination,Python,permutations,list,print,length,Permutation,combinations,input 来源: https://www.cnblogs.com/xxxsans/p/13854332.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。