Python数据分析(中英对照)·Random Choice 随机选择

作者 : jamin 本文共1690个字,预计阅读时间需要5分钟 发布时间: 2020-10-18 共1126人阅读

1.1.5: Random Choice 随机选择

通常,当我们使用数字时,偶尔也会使用其他类型的对象,我们希望使用某种类型的随机性。
Often when we’re using numbers, but also,occasionally, with other types of objects,we would like to do some type of randomness.
例如,我们可能想要实现一个简单的随机抽样过程。
For example, we might want to implement a simple random sampling process.
为此,我们可以使用随机模块。
To this end, we can use the random module.
所以,我们的出发点是,再次导入这个模块,random。
So the starting point is, again, to import that module, random.
让我们考虑一个简单的例子,其中列表中包含一组数字,我们希望从这些数字中随机统一选择一个。
Let’s think about a simple example where we have a set of numbers contained in a list,and we would like to pick one of those numbers uniformly at random.
在本例中,我们需要使用的函数是random.choice,在括号内,我们需要一个列表。
The function we need to use in this case is random.choice,and inside parentheses, we need a list.
在这个列表中,我将只输入几个数字——2、44、55和66。
In this list, I’m going to just enter a few numbers– 2, 44, 55, and 66.
然后,当我运行随机选择时,Python会将其中一个数字返回给我。
And then when I run the random choice, Python returns one of these numbers back to me.
如果我重复同一行,我会得到一个不同的答案,因为Python只是随机选取其中一个对象。
If I repeat the same line, I’m going to get a different answer,because, again, Python is just picking one of those objects at random.
关于随机选择方法,需要了解的一个关键点是Python并不关心所使用对象的基本性质
A crucial thing to understand about the random choice method is that Python doesn’t care about the fundamental nature of the objects that
都包含在该列表中。
are contained in that list.
这意味着,不用数字,我也可以从几个字符串中选择一个。
What that means, instead of using numbers,I could also be choosing one out of several strings.
让我们看看这是怎么回事。
So let’s see how that might work.
我要回到我的清单上。
I’m going to go back to my list.
我只想在这里包括三个短字符串。
I’m just going to include three short strings here.
让我们只做“aa”,“bb”和“cc”
Let’s just do "aa," "bb," and "cc."
我可以让Python随机选择其中一个。
I can ask Python to pick one of these uniformly at random.
因此Python并不关心这些对象的性质。
So Python doesn’t care about the nature of these objects.
对于任何类型的对象,随机的工作方式都是一样的。
Random works just the same way for any type of object.

本站所提供的部分资源来自于网络,版权争议与本站无关,版权归原创者所有!仅限用于学习和研究目的,不得将上述内容资源用于商业或者非法用途,否则,一切后果请用户自负。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源。如果上述内容资对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。本站不保证所提供下载的资源的准确性、安全性和完整性,源码仅供下载学习之用!如用于商业或者非法用途,与本站无关,一切后果请用户自负!本站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。如有侵权、不妥之处,请联系站长以便删除!
金点网络 » Python数据分析(中英对照)·Random Choice 随机选择

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
是否提供免费更新服务?
持续更新,永久免费
是否经过安全检测?
安全无毒,放心食用

提供最优质的资源集合

立即加入 友好社区
×