zoukankan      html  css  js  c++  java
  • python基础的练习题1道

    题目1:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
    import  itertools
    list5=[1,2,3,4]
    newiter=itertools.permutations(list5,3)      #这个能满足要求,A(4,3)=(4)!/(4-3)!=4*3*2*1/1
    for i in newiter:
        print(i)
    
    '''
    (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)
    '''

    【编程小结】

    1、复制list的注意事项

    mlist=[1,2,3,5]
    tlist=mlist.copy()      #说明:这里tlist与mlist分别指向两个对象(把mlist指向的对象copy一份,tlist指向copy对象)
    i=tlist.pop(0)          
    print('tlist', tlist)   #tlist [2, 3, 5]
    print('mlist', mlist)   #mlist [1, 2, 3, 5]
    
    mlist=[1,2,3,5]
    tlist=mlist            #若用tlist=mlist,则tlist指向和mlist一样
    i=tlist.pop(0)
    print('tlist', tlist)   #tlist [2, 3, 5]
    print('mlist', mlist)   #mlist [2, 3, 5]

     2、python中itertools的使用,参照:http://www.10qianwan.com/articledetail/35968.html

  • 相关阅读:
    05:背景设置
    04:文本 + 字体
    03:CSS三大特性
    02:Emmet 语法 + Ps切图
    01:基本概念 + 引入CSS
    input禁止输入的方法
    十进制转换成任意进制(栈的应用)
    假定一个解并判断是否可行(二分搜索应用)
    关于二分查找和二分搜索
    统计单词
  • 原文地址:https://www.cnblogs.com/ww-xiaowei/p/12582773.html
Copyright © 2011-2022 走看看