Python Case 001 (four digits: 1, 2, 3, 4, how many distinct three digits without duplicates) __python

Source: Internet
Author: User

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 ""


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.