• 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. 输出效果

     

  • 相关阅读:
    04.sys
    leetcode算法-加油站
    Spring动态AOP
    (java反射-JDK动态代理)+CGLIB动态代理
    java反射-基础语法
    leetcode算法-验证回文串
    leetcode算法-盛最多水的容器
    leetcode算法-两数之和
    leetcode算法-三数之和
    leetcode算法-最长和谐子序列
  • 原文地址:https://www.cnblogs.com/AHappyBird/p/9340841.html
Copyright © 2020-2023  润新知