我正在编写一个程序,我需要从 python 中的 list
中打乱 string
的字母。例如我有一个 list
的 string
像:
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/