Python outputs a three-digit number consisting of 1, 2, 3, and 4, which are inconsistent and have no duplicates.
Question: There are four numbers: 1, 2, 3, and 4. How many three numbers can be composed of different and non-repeated numbers? What is each?
Program analysis: the numbers that can be filled in hundreds, ten, and one digit are 1, 2, 3, and 4. Make up all the sorting before dropping the sorting that does not meet the conditions.
Program source code
Method 1:
#!/usr/bin/python# -*- coding: UTF-8 -*- for i in range(1,5): for j in range(1,5): for k in range(1,5): if( i != k ) and (i != j) and (j != k): print i,j,k
The above instance output is as follows:
1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2
Method 2:
Remove duplicate elements with a set
#! /Usr/bin/env python #-*-coding: UTF-8-*-import pprintlist_num = ['1', '2', '3 ', '4'] list_result = [] for I in list_num: for j in list_num: for k in list_num: if len (set (I + j + k) = 3: list_result + = [int (I + j + k)] print ("three digits that can make up % d different and have no repeated numbers:" % len (list_result) pprint. pprint (list_result)
The output result is the same as the above, but one thing is output in the form of a set.
Summary
The above section describes all the three-digit numbers output by Python consisting of 1, 2, 3, and 4, which are different from each other and have no duplicates. I hope this will help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!