gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:34:10 26 4
gpt4 key购买 nike

我正在编写一个程序,我需要从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com