Python随机数与随机字符串详解
随机整数:>>>importrandom>>>random randint(0,99)21随机选取0到100间的偶数:>>>importrandom>>>random randrange(0,101,2)42随机浮点数
随机整数:
|
1
2
3
|
>>> import random>>> random.randint(0,99)21 |
随机选取0到100间的偶数:
|
1
2
3
|
>>> import random>>> random.randrange(0, 101, 2)42 |
随机浮点数:
|
1
2
3
4
5
|
>>> import random>>> random.random() 0.85415370477785668>>> random.uniform(1, 10)5.4221167969800881 |
随机字符:
|
1
2
3
|
>>> import random>>> random.choice('abcdefg&#%^*f')'d' |
多个字符中选取特定数量的字符:
|
1
2
3
|
>>> import randomrandom.sample('abcdefghij',3) ['a', 'd', 'b'] |
多个字符中选取特定数量的字符组成新字符串:
|
1
2
3
4
5
|
>>> import random>>> import string>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).replace(" ","")'fih' |
随机选取字符串:
|
1
2
3
|
>>> import random>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )'lemon' |
洗牌:
|
1
2
3
4
5
|
>>> import random>>> items = [1, 2, 3, 4, 5, 6]>>> random.shuffle(items)>>> items[3, 2, 5, 6, 4, 1] |
random的函数还有很多,此处不一一列举,
Python随机数与随机字符串详解的更多相关文章
- python生成随机数、随机字符串
python生成随机数.随机字符串 import randomimport string # 随机整数:print random.randint(1,50) # 随机选取0到100间的偶数:print ...
- Python变量和字符串详解
Python变量和字符串详解 几个月前,我开始学习个人形象管理,从发型.妆容.服饰到仪表仪态,都开始做全新改造,在塑造个人风格时,最基础的是先了解自己属于哪种风格,然后找到参考对象去模仿,可以是自己欣 ...
- [转]使用python来操作redis用法详解
转自:使用python来操作redis用法详解 class CommRedisBase(): def __init__(self): REDIS_CONF = {} connection_pool = ...
- Python 单向队列Queue模块详解
Python 单向队列Queue模块详解 单向队列Queue,先进先出 '''A multi-producer, multi-consumer queue.''' try: import thread ...
- random and password 在Linux下生成crypt加密密码的方法,shell 生成指定范围随机数与随机字符串
openssl rand -hex n (n is number of characters) LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head ...
- Python中的高级数据结构详解
这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...
- Python中格式化format()方法详解
Python中格式化format()方法详解 Python中格式化输出字符串使用format()函数, 字符串即类, 可以使用方法; Python是完全面向对象的语言, 任何东西都是对象; 字符串的参 ...
- Python包模块化调用方式详解
Python包模块化调用方式详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一般来说,编程语言中,库.包.模块是同一种概念,是代码组织方式. Python中只有一种模块对象类型 ...
- python中的tcp示例详解
python中的tcp示例详解 目录 TCP简介 TCP介绍 TCP特点 TCP与UDP的不同点 udp通信模型 tcp客户端 tcp服务器 tcp注意点 TCP简介 TCP介绍 TCP协议 ...
随机推荐
- C#堆栈原理(我有两个例子测试你到底会不会)
背景 上次写了一篇文章关于try finnally的一些疑问(被我用windows live覆盖了,草),后来经过大神们解释,我明白了在我理解了try.finnally运行原理后,还欠缺的就是关于值类 ...
- 【HDU3487】【splay分裂合并】Play with Chain
Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...
- cocos2d-x编译错误问题
在xcode中创建的cocos2d-x项目,然后添加了一个基类,里面有虚方法,编译时出错,错误如下: Undefined symbols for architecture x86_64: " ...
- shell用if
--------- shell用if出错了,Why? shell if 实例: site=github.com/fankcoder if [ $site == github.com/fankcoder ...
- 初涉JavaScript模式 (3) : 字面量
什么是字面量? 在编程语言中,字面量是一种表示值的记法.例如,"Hello, World!" 在许多语言中都表示一个字符串字面量(string literal ),JavaScri ...
- 一站式远程页面调试工具spy-debugger 2.0,已支持HTTPS
项目名称: spy-debugger 项目地址:https://github.com/wuchangming/spy-debugger 关于spy-debugger npm Build Status ...
- TatukGIS - GisDefs - CanonicalSQLName 函数
函数名称 CanonicalSQLName 所在单元 GisDefs 函数原型 function CanonicalSQLName(const _name: String; const _tem ...
- action 关联
<act_window context="{'product_id': active_id}" id="act_stock_product_location_ope ...
- QLineEdit
The QLineEdit widget is a one-line text editor. Header: #include <QLineEdit> qmake: QT += widg ...
- iOS 检测版本更新
如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bundle versio ...