菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python 练习实例1

Python 100例 Python 100例

题目:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。

程序源代码:

实例

#!/usr/bin/python# -*- coding: UTF-8 -*-foriinrange(1,5): forjinrange(1,5): forkinrange(1,5): if(i != k)and(i != j)and(j != k): print(i,j,k)

以上实例输出结果为:

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

Python 100例 Python 100例

AI 思考中...

16 篇笔记 写笔记

  1. #0

    zavier

    126***[email protected]

    158

    使用列表形式,并计算总结:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    # 原答案没有指出三位数的数量,添加无重复三位数的数量
    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 "总数量:", len(d)
    print d
    

    zavier

    126***[email protected]

    9年前 (2017年04月13日)
  2. #0

    盼盼

    946***[email protected]

    96

    将for循环和if语句综合成一句,直接打印出结果

    #!/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 (list)
    

    盼盼

    946***[email protected]

    9年前 (2017年04月16日)
  3. #0

    习惯乌龙茶

    rea***[email protected]

    40

    参考方法(设置最大,最小值):

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    line=[]
    for i in range(123,433):
     a=i%10
     b=(i%100)//10
     c=(i%1000)//100
     if a!=b and b!=c and a!=c and 0<a<5 and 0<b<5 and 0<c<5 :
     print (i)
     line.append(i)
    print('the total is :',len(line))
    

    习惯乌龙茶

    rea***[email protected]

    9年前 (2017年04月20日)
  4. #0

    python3 下参考方案:

    #!/usr/bin/env python3
    #coding:utf-8
    num=[1,2,3,4]
    i=0
    for a in num:
     for b in num:
     for c in num:
     if (a!=b) and (b!=c) and (c!=a):
     i+=1
     print(a,b,c)
    print('总数是:',i) 
    
    9年前 (2017年04月24日)
  5. #0

    白色帽子

    liu***[email protected]

    24

    参考方法:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    #用集合去除重复元素
    import pprint
    list_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("能组成%d个互不相同且无重复数字的三位数: "%len(list_result))
    pprint.pprint(list_result)
    

    白色帽子

    liu***[email protected]

    9年前 (2017年05月22日)
  6. #0

    Chyroc

    che***[email protected]

    101

    python自带这个函数的

    #!/usr/bin/env python3
    #coding:utf-8
    from itertools import permutations
    for i in permutations([1, 2, 3, 4], 3):
     print(i)

    Chyroc

    che***[email protected]

    9年前 (2017年05月31日)
  7. #0

    weapon

    965***[email protected]

    21

    补充一下:

    #!/usr/bin/env python3
    # -*- coding:utf-8 -*-
    #补充一下
    from itertools import permutations
    for i in permutations([1, 2, 3, 4], 3):
     k = ''
     for j in range(0, len(i)):
     k = k + str(i[j])
     print (int(k))

    weapon

    965***[email protected]

    9年前 (2017年06月07日)
  8. #0

    逸章

    me@***gdewen.com

    18

    没事找事之位运算

    # coding:utf-8
    #从 00 01 10 到 11 10 01
    for 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

    逸章

    me@***gdewen.com

    9年前 (2017年06月13日)
  9. #0

    DCGDDD

    805***[email protected]

    41

    考虑减少冗余判断和循环,做如下优化;

    #!/usr/bin/python3
    for i in range(1, 5):
     for j in range(1, 5):
     if (j==i) :
     continue;
     for k in range(1, 5):
     if (k==i or k==j):
     continue;
     print(i,j,k);

    DCGDDD

    805***[email protected]

    9年前 (2017年08月13日)
  10. #0

    嘿嘿

    123***.com

    19

    Python3 测试实例:

    #!/usr/bin/python3
    list = [1,2,3,4]
    for i in list:
     list1 = list.copy()
     list1.remove(i)
     for j in list1:
     list2 = list1.copy()
     list2.remove(j)
     for k in list2:
     print(i, j, k)
    

    嘿嘿

    123***.com

    9年前 (2017年09月07日)
  11. #0

    Krystal

    104***[email protected]

    11

    加入了format函数

    #!/usr/bin/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 ( i != j and i != k and j != k)]
    d = len(list)
    print('1,2,3,4能组成 %d 个互不相同且无重复数字的三位数。' % d)
    print('他们各是:%s' % list)

    Krystal

    104***[email protected]

    9年前 (2017年09月25日)
  12. #0

    红烧土豆块

    545***[email protected]

    18

    数量统计用个做自加就够了

    #!/usr/bin/env python3
    #coding=utf-8
    from itertools import permutations
    t = 0
    for i in permutations('1234',3):
     print(''.join(i))
     t += 1
    print("不重复的数量有:%s"%t)

    红烧土豆块

    545***[email protected]

    9年前 (2017年09月27日)
  13. #0

    这个好好玩

    303***[email protected]

    15

    参考:

    #encoding=utf8
    #有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
    d = []
    for j in range(1,5):
     for k in range(1,5):
     for l in range(1,5):
     if l!=j!=k!=l:
     d.append(int(str(j)+str(k)+str(l)))
    print d
    print len(d)

    这个好好玩

    303***[email protected]

    9年前 (2017年11月07日)
  14. #0

    XMDERO

    124***[email protected]

    12
    #直接用列表推导式
    [(x,y,z) for x in range(1,5) for y in range(1,5) for z in range(1,5) if(x!=y)and(x!=z)and(y!=z)]

    XMDERO

    124***[email protected]

    9年前 (2017年11月16日)
  15. #0

    阳光不锈

    173***[email protected]

    24

    参考方法:

    #coding=utf-8
    print("----------递归法 -------------")
    #递归法 
    def f01(i):
     if i==123:
     print(i)
     return
     else:
     if (set('567890') & set(str(i))==set()) and (len(set(str(i)))==3):
     print(i)
     f01(i-1)
    f01(432)
    print("----------生成器法-------------")
    #生成器法
    def f02():
     for i in range(123,433):
     if (set('567890') & set(str(i))==set()) and (len(set(str(i)))==3):
     yield i
    for i in f02():
     print(i)

    阳光不锈

    173***[email protected]

    8年前 (2018年03月04日)
  16. #0

    whitestonex

    851***[email protected]

    23

    参考方法:

    import itertools
    DataIn = list('1234')
    TmpList = []
    for x in list(itertools.combinations(DataIn,3)):
     TmpList = TmpList + list(itertools.permutations(x,3))
    for i in TmpList:
     print(''.join(i))

    whitestonex

    851***[email protected]

    8年前 (2018年03月13日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /