有没有办法在python中加扰字符串? [英] Are there any ways to scramble strings in python?

查看:151
本文介绍了有没有办法在python中加扰字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a program and I need to scramble the letters of strings from a list in python. For instance I have a list of strings 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆