Python choice() 函数

Python 教程 · 2019-02-01 23:29:43

描述

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 教程 文章: https://www.codercto.com/courses/l/8.html

查看所有标签

Mastering Bitcoin

Mastering Bitcoin

Andreas M. Antonopoulos / O'Reilly Media / 2014-12-20 / USD 34.99

Mastering Bitcoin tells you everything you need to know about joining one of the most exciting revolutions since the invention of the web: digital money. Bitcoin is the first successful digital curren......一起来看看 《Mastering Bitcoin》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换