The topic content comes from the network, joins the individual understanding the process, and reviews
#!/usr/bin/python #-*-Coding:utf-8-*-#Author: Wumi #题目: There are four numbers: 1, 2, 3, 4, to make up the number of three digits that are not identical and have no duplicate numbers.
Each is how much. #程序分析: Can be filled in hundreds, 10 digits, single-digit numbers are 1, 2, 3, 4.
Make up all the permutations and then remove the arrangement that does not satisfy the condition. # If there is a 0 in four digits, the difficulty is slightly increased num =[] # list for I in range (1,5): # The high level of range is not included for J in Range (1,5): for K in R Ange (1,5): If I!=j and I!= K and j!=k:print i,j,k print k,j,i # reverse Nu M Num.append ([i,j,k]) print "How many num we have of the permutation and combination:", Len (num) # 24 # Big tall up #将for循环和if语句综合成一句, print out the results directly lists =[(I*100+10*J+K) for I in range (1,5) for the J in range (1,5) for K in range (1,5 ) if (I!=j and I!=k and j!=k)] Print len (lists) print Len (lists), ":", lists # Use Set function to remove the duplicate Numbers, set is the # One important with no duplicate data, the alphabet can uses join function + alphabetlist = [' 1 ', ' 2 ', ' 3 ', ' 4 '] numberlist = [Int (i+j+k) for I-Alphabetlist for J-Alphabetlist for K-in Alphabetlist if (Len (set i+j+k)] ==3] [numberlist], ":", Len (numberlist) # question is why I!= K and!= J? Which is why there is no duplicate bit. such as "122" and "Here" is the core reason:: Lists =[' 1 ', ' 2 ', ' 3 ' "For I in Lists:for J in Lists:print set (i
+J) # Add memeber into set using Plus +---> Set ([' 1 ', ' 2 ']), no replicate data print int (i+j) "" "Set ([' 1 ']) One set ([' 1 ', ' 2 ']) set ([' 1 ', ' 3 ']) set ([' 1 ', '] 2 ']) set ([' 2 ']) set ([' 3 ', ' 2 ']) set ([' 1 ', ' 3 ']) set ([' 3 ')]
' 2 ']) ([' 3 ']) 33 ""