Python choice() 函数

栏目: 编程语言 · Python · 编程教程 · python 教程 · 发布时间: 7年前

内容简介:choice() 方法返回一个列表,元组或字符串的随机项。

描述

choice() 方法返回一个列表,元组或字符串的随机项。

语法

以下是 choice() 方法的语法:

import random

random.choice( seq  )

注意:choice()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。

参数

  • seq -- 可以是一个列表,元组或字符串。

返回值

返回随机项。

实例

以下展示了使用 choice() 方法的实例:

#!/usr/bin/python
import random

print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9])
print "choice('A String') : ", random.choice('A String')

以上实例运行后输出结果为:

choice([1, 2, 3, 5, 9]) :  2
choice('A String') :  n

以上所述就是小编给大家介绍的《Python choice() 函数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Development Recipes

Web Development Recipes

Brian P. Hogan、Chris Warren、Mike Weber、Chris Johnson、Aaron Godin / Pragmatic Bookshelf / 2012-1-22 / USD 35.00

You'll see a full spectrum of cutting-edge web development techniques, from UI and eye candy recipes to solutions for data analysis, testing, and web hosting. Make buttons and content stand out with s......一起来看看 《Web Development Recipes》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

SHA 加密
SHA 加密

SHA 加密工具