【问题标题】:Python - getting 9 random words from an arrayPython - 从数组中获取 9 个随机单词
【发布时间】:2015-03-11 12:48:44
【问题描述】:

我有一个包含 10 个单词的数组,我需要取出 9 个不一样的单词;并将它们放入另一个数组中。 数组: 数组 = [“夜晚”、“烟雾”、“幽灵”、“牙齿”、“关于”、“骆驼”、“棕色”、“有趣”、“椅子”、“价格”] 8 位 swog swig

【问题讨论】:

标签: arrays


【解决方案1】:
import random

array = ["night", "smoke", "ghost", "tooth", "about", "camel", "brown", "funny", "chair", "price"]
new_array = random.sample(array, 9)

附言

列表不是数组。 Python 有内置数组,但很少使用。

【讨论】:

    猜你喜欢
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多