zoukankan      html  css  js  c++  java
  • python_生成随机数与列表排序

    1.  列表排序可采用两种方法

    sorted(list) 直接改变list

    调用list的方法 list.sort

    2.

    random.randint(a,b) 生成大于等于a小于等于b的整数

    random.random() 生成一个在[0,1)区间上的实数

    random.choice(sequence) sequence泛指list、tuple、字符串等

    random.randrange(start,stop,step) step务必给出,在本次实践中,如果不给出将会随机给一个值且极大可能为负数

    具体参考 https://www.cnblogs.com/whiteprism/p/6290814.html

    3. 代码实例

    导入模块 import random

    def FetchRandom():
        '生成随机数列表并从中抓取'
        N1 = random.randint(2,100)
        N2 = random.randint(1,100)
        while N2>N1:
            N2 = random.randint(1,100)
        alist = []
        for i in range(N1):
            n = random.randint(0, 2 ^ 31 - 1)
            alist.append(n)
        blist = []
        for i in range(N2):
            blist.append(random.randrange(alist[0],alist[N1-1],1))
         #blist.append(random.choice(alist))  print sorted(blist) print 'N1=',N1,' N2=',N2,' n=',n

    4. 输出效果

     

  • 相关阅读:
    测试方法与步骤
    团队项目需求分析
    第一次个人作业
    3种shell自动交互的方法
    mysql用户管理
    build web application with golang
    安卓中的LINUX内核
    结对项目小结
    关于aria2-yaaw下载软件
    软工结对项目预览
  • 原文地址:https://www.cnblogs.com/AHappyBird/p/9340841.html
Copyright © 2011-2022 走看看