【问题标题】:How To create an anagram of a word?如何创建一个单词的字谜?
【发布时间】:2016-02-01 15:28:08
【问题描述】:

如何使用 random.shuffle 对单词进行字谜?到目前为止,我收到了一条错误消息:非常感谢任何帮助:

    import random
    word = "house"
    random.shuffle(word)
    print(word)

【问题讨论】:

    标签: python python-3.x anagram


    【解决方案1】:

    随机模块中的函数 shuffle 就地随机播放,因此您首先必须将字符串转换为字符列表,对其进行随机播放,然后再次加入结果。

    import random
    word = "house"
    l = list(word)
    random.shuffle(l)
    result = ''.join(l)
    print(result)
    

    【讨论】:

    • 如果字符串是纯ASCII,也可以使用bytearray:word_ = bytearray(word, 'ASCII'); random.shuffle(word_); result = word_.decode('ASCII')
    猜你喜欢
    • 1970-01-01
    • 2011-09-07
    • 2015-07-26
    • 2016-05-21
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2014-08-06
    相关资源
    最近更新 更多