zoukankan      html  css  js  c++  java
  • python random

    In [120]: import random
    
    In [121]: l1 = (1, 2, 3)
    
    In [122]: l2 = [1, 2, 3]
    
    In [123]: ran
    random  range   
    
    In [123]: random.shuffle(l1)
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-123-00db8a674af6> in <module>()
    ----> 1 random.shuffle(l1)
    
    /usr/lib/python2.7/random.pyc in shuffle(self, x, random)
        287             # pick an element in x[:i+1] with which to exchange x[i]
        288             j = _int(random() * (i+1))
    --> 289             x[i], x[j] = x[j], x[i]
        290 
        291     def sample(self, population, k):
    
    TypeError: 'tuple' object does not support item assignment
    
    In [124]: random.shuffle(l2)                                                                    
    
    In [125]: l2
    Out[125]: [3, 2, 1]

    In [126]: a = l2
    
    In [127]: a
    Out[127]: [3, 2, 1]
    
    In [128]: a = random.shuffle(l2)
    
    In [129]: a
    
    In [130]: a
    
    In [131]: print a
    None

    上面的错误是因为元组内容不可更改

    下面返回a的值是None是因为shuffle返回值的原因 ,shuffle只是在原基础上做的打乱

    In [19]: import random
    
    In [20]: random.random()
    Out[20]: 0.9467622073347552
    
    In [21]: random.uniform(1, 2)
    Out[21]: 1.5264479206993298
    
    In [22]: random.uniform(1, 1)
    Out[22]: 1.0
    
    In [23]: random.randint(1,3)
    Out[23]: 1
    
    In [24]: random.randrange(1, 100)
    Out[24]: 7
    
    In [25]: random.randrange(1, 100, 3)
    Out[25]: 22
    
    In [26]: random.randrange(1, 100, 3)
    Out[26]: 28
    
    In [27]: random.randrange(1, 100, 3)
    Out[27]: 58
    
    In [28]: random.randrange(1, 100, 3)
    Out[28]: 91
    
    In [29]: random.choice(range(10))
    Out[29]: 5
    
    In [30]: random.sample(range(10), 3)
    Out[30]: [9, 6, 2]
    

      

  • 相关阅读:
    Visual Studio 2008中文正式版
    属性控制类
    PowerDesigner11技巧
    OPENXML用法
    .net下载文件
    Merge Into 语句代替Insert/Update在Oracle中的应用实战
    PowerDesigner中使用vbscript访问对象进行批量操作
    转:SubSonic介绍和相关文章
    C# 多线程控制控件实例(例程简单,注释详细)
    学习SubSonic的笔记 Version 2.1
  • 原文地址:https://www.cnblogs.com/wswang/p/5439774.html
Copyright © 2011-2022 走看看