+ 2

How do I iterate between 5 digits and return the 4 digits without duplicated digits

Input_unit = [1,2,3,4,5] Output_digit = 1,2,3,4 1,2,3,5 1,2,4,3 1,2,4,5 1,2,5,3 1,2,5,4 . . . No two digits must be unique in output result

17th May 2020, 5:58 PM
Jolly Ayedun
Jolly Ayedun - avatar
4 Respuestas
+ 3
Things like this can be done with module itertools and function permutations. permutations(<sequence>, count) <sequence> can be a list with all input characters (in your case: 1,2,3,4,5), count defines the number of characters used for each output the permutations generates. With your settings it eill generate 120 different outputs. The output is a permutation object, that can be converted to a list.
17th May 2020, 6:07 PM
Lothar
Lothar - avatar
+ 2
Yes you can, but I am afraid these versions will be more difficult to understand than itertools with permutation. You can use: - nested loops - recursion
17th May 2020, 7:21 PM
Lothar
Lothar - avatar
+ 1
Thanks Lothar... I'm a phyton beginner. Can i achieve this without using module?
17th May 2020, 6:44 PM
Jolly Ayedun
Jolly Ayedun - avatar
+ 1
Here it is b=[1,2,3,4,5] a=list((i,j,k,l) for i in b for j in b for k in b for l in b) def appearance(i): b={} count=0 for i in a: if i not in b: count+=1 b[i]=count elif i in b: for l,m in b.items(): m+=1 b[l]=m m=0 count=0 if max(b.values())>1: return 2 else: return 1 for i in a: if appearance(i)==1: for j in str(i): if j!="(" and j!=")": print(j,end="") print("")
17th May 2020, 7:36 PM
Abhay
Abhay - avatar