The most distressing thing in the world was the notes I used to make before I couldn't find them. Alas, years of painstaking efforts, are all experience-derived
title: There are four numbers: 1, 2, 3, 4, how many different and no repetition of the number of three digits? What's the number?
Program Analysis: can be filled in hundreds, 10 digits, single digit numbers are 1, 2, 3, 4. Make all the permutations and then remove the permutations that do not meet the criteria.
Program Source code:
Instance (Python 2.0+)#!/usr/bin/python #-*-Coding:utf-8-*- For I Inch Range(1,5):For J Inch Range(1,5):For K Inch Range(1,5):If( i! = k and (i! = j and (j! = k< Span class= "Hl-brackets" >) : print i,j,k
The result of the above example output is:
1 2 31 2 41 3 21 3 41 4 21 4 32 1 32 1 42 3 12 3 42 4 12 4 33 1 23 1 43 2 13 2 43 4 13 4 24 1 24 1 34 2 14 2 34 3 14 3 2
Python 100 Cases
Python JSONList of Notes
-
Zavier
[email protected]
Use list form and calculate summary:
#!/usr/bin/python#-*-coding:utf-8-*-# The original answer does not indicate the number of three digits, add the number of non-repeating three digits d=[]for a in range (1,5 ): for B in range (1,5): for C in range (1,5): if (a!=b) and (A!=c) and (c!=b): D.append ([a,b,c]) print "Total quantity:", Len (d) Print D
zavier zavier
[email protected]
3 months ago (04-13)
-
Hope
[email protected]
Synthesize the For loop and if statement into a single sentence, printing the result directly
#!/usr/bin/env python#-*-coding:utf-8-*-list_num = [1,2,3,4] List = [i*100 + j*10 + K for I in List_num for J in List_num for K in List_num if (j! = I and k! = J and K! = i)]print (l IST)
hope Hope
[email protected]
Span class= "Comt-author" > 3 months ago (04-16)
-
Get used to oolong tea
[email protected]
Reference method (set maximum, minimum value) :
#!/usr/bin/python#-*-coding:utf-8-*-line=[]for i in Range (123,433): a=i%10b= (i%100)/ /10c= (i%1000)//100if a!=b and B!=c and A!=c and 0<a<5 and 0<b<5 and 0<c<5:p rint (i) line.append (i) Print (' The total was: ', Len (line) ')
custom Oolong tea customary oolong tea
[email protected]
3 months ago (04-20)
-
Chengko
[email protected]
reference address
Python3 under reference scheme:
#!/ Usr/bin/env python3#coding:utf-8num=[1,2,3,4]i=0for A in num:for B in Num:for C in Num:if (A!=b) A nd (B!=C) and (c!=a): I+=1 print (a,b,c) print (' Total number is: ', i)
class= "fa fa-user" > Chengko
[email protected"
Reference address
3 months ago (04-24)
-
Whaike
[email protected]
A more pythonic way:
#-*-coding:utf-8-*-for i in range (1,5): for j in Range (1,5): A for k in range ( 1,5): If I!=j!=k:print i,j,k
whaike Span class= "FA fa-user" > whaike
[email protected]
2 months ago (05-17)
-
White hat
[email protected]
Reference method:
#!/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 ("capable of composing%d three digits with distinct and no duplicate numbers:"%len (List_result)) Pprint.pprint (List_result) white hat White hat
[email protected]
2 months ago (05-22)
-
Chyroc
[email protected]
Python comes with this function.
#!/usr/bin/env python3#coding:utf-8from itertools import permutationsfor i in Permutations ([1, 2, 3, 4], 3): Print (i)
chyroc chyroc
[email protected]
1 months ago (05-31)
-
weapon
[email protected]
Add:
#!/usr/bin/env python3#-*-coding:utf-8-*-#补充一下from itertools Import Permutationsfor i in permutations ([1, 2, 3, 4], 3): K = "for J in range (0, Len (i)): K = k + str (I[J]) pr int (int (k)))
weapon weapon
[email protected]
1 months ago (06-07)
Yi Zhang
[Email protected]
It's okay.
# coding:utf-8# to 01for num in range (6,58): a = num >> 4 & 3 b = num >> 2 & 3 c = num & 3 if ((a^b) and (B^c) and (C^a)): print a+1,b+1,c+1
Www.pdfxs.com
www.123lala.cn
Most distressing previously made notes Python practice Example 1