1 # Title: There are 1, 2, 3, 4 digits, how many different and no repetition of the number of three digits? How much are they?
2 num = [1,2,3,4] 3 n = [] 4 for x in num: 5 for y in num: 6 for z in num: 7 if x != y and x != z and y != z: 8 m = x*100 + y*10 + z 9 n.append (m) 10 11 print (' Can make up a non-repeating three-digit number with%d '% (len (n))) 12 print (' They are: ') 13 print (n)
Operation Result:
[[email protected] code_100]# Python code_1.py can consist of three digits without duplicates with 24 of them are: [123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241 , 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]
Script Explanation:
2 num = [1,2,3,4] # Define these four numbers as a list for the for traversal of these numbers, or you can use rang (1,5) To represent the 3 n = [] #定义一个空list, used to store the required number, the list is used in order to use the Len () function to take the length 4 for x in num: #用三个for循环来遍历所有可能的组合 5 for y in num: 6 for z in num: 7 if x != y and x != z and y != z: # exclude three digits with duplicate numbers (I don't know if there are any simple expressions to express this condition, not much) 8 m = x*100 + y*10 + z #取得符合要求的数字 9 &Nbsp;n.append (m) #将符合要求的数字用append方法添加到列表n中 10 11 print ( ' Can compose a non-repeating three-digit number with%d '% (len (n)) ') # format the output list length, which is the number of lists 12 print (' They are: ') 13 print (n) # lists the numbers that meet the requirements, or you can use the For loop to iterate through the list and take out the numbers
This article is from the "Learning Notes" blog, so be sure to keep this source http://netsyscode.blog.51cto.com/6965131/1742044
Introduction to programming language Classic 100 cases--1