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://codercto.com/courses/l/8.html

查看所有标签

Designing Programmes

Designing Programmes

Karl Gerstner / Springer Verlag / 2007 / $499.00

Karl Gerstnera (TM)s work is a milestone in the history of design. One of his most important works is Designing Programmes, which is presented here in a new edition of the original 1964 publication. I......一起来看看 《Designing Programmes》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

MD5 加密
MD5 加密

MD5 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试