有没有办法在python中加扰字符串? [英] Are there any ways to scramble strings in python?
本文介绍了有没有办法在python中加扰字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个程序,我需要从python中的list
扰乱string
的字母.例如,我的list
为string
,例如:
I'm writing a program and I need to scramble the letters of string
s from a list
in python. For instance I have a list
of string
s like:
l = ['foo', 'biology', 'sequence']
我想要这样的东西:
l = ['ofo', 'lbyoogil', 'qceeenus']
最好的方法是什么?
感谢您的帮助!
推荐答案
Python包含电池.
Python has batteries included..
>>> from random import shuffle
>>> def shuffle_word(word):
... word = list(word)
... shuffle(word)
... return ''.join(word)
列表理解是创建新列表的简便方法:
A list comprehension is an easy way to create a new list:
>>> L = ['foo', 'biology', 'sequence']
>>> [shuffle_word(word) for word in L]
['ofo', 'lbyooil', 'qceaenes']
这篇关于有没有办法在python中加扰字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文