python - 有没有什么方法可以在 python 中打乱字符串?

标签 python string list scramble

我正在编写一个程序,我需要从 python 中的 list 中打乱 string 的字母。例如我有一个 liststring 像:

l = ['foo', 'biology', 'sequence']

我想要这样的东西:

l = ['ofo', 'lbyoogil', 'qceeenus']

最好的方法是什么?

感谢您的帮助!

最佳答案

Python 自带电池..

>>> from random import shuffle

>>> def shuffle_word(word):
...    word = list(word)
...    shuffle(word)
...    return ''.join(word)

列表理解是创建新列表的一种简单方法:

>>> L = ['foo', 'biology', 'sequence']
>>> [shuffle_word(word) for word in L]
['ofo', 'lbyooil', 'qceaenes']

关于python - 有没有什么方法可以在 python 中打乱字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6181304/

相关文章:

java - 使用其他语言而不是英语作为java字符串(以波斯语为例)

python - 根据部分字符串在列表中查找条目

python - 如何在 GAE 上最好地设计日期/地理邻近查询?

string - Matlab能否去掉URL中的路径,只留下域名部分?

python - 获取创建的对象将被赋予的属性名称

python - 使用递归在python中获取子字符串

java - 线程中的异常 "main"java.lang.IndexOutOfBoundsException : Index: 3, 大小:2

python - 可以间接修改字典吗?

Python HTTP 请求 URL 结束斜杠

python - 基于图形的元组合并?