4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

文字列シャッフルワンライナー

Posted at

Python でのシャッフルは random.shuffle(list) でやりますが文字列でやろうとすると怒られるので一度 list(strings) しないといけなくて しかも None が返ってくるのでめちゃくちゃだるい!死んじゃう!となっていたのですが random.sample() を知ったので生きる!!となりました
random.sample() は対象からピックアップして任意の長さのリストを返してくれるので文字列で受け取る lambda expression にするとこんな感じ

Supercalifragilisticexpialidocious
Supercalifragilisticexpialidocious = lambda x: ''.join(random.sample(x ,len(x))

ワンライナー書くなら
s='strings';''.join(__import__('random').sample(s,len(s)));

>>> for _ in 'Supercalifragilisticexpialidocious':
...     print(Supercalifragilisticexpialidocious('Supercalifragilisticexpialidocious'))
...
euirfetxipsailigusodlrlcaiipciSaco
Sodirupiliasgsiurfolapicceexitailc
ooiiscxeiurialaaceldgprpfitiulcisS
igiSaclepilcespuairioxcloaustirdif
iclcfllguporastecSxipiierodaiiausi
Saticaausridlfeocolcilpiipigxureis
Siitreasieaailirloccgsofxluudciipp
iaceliufaStrgiuiroceaxlosppiilidsc
scarllreoiiscdoxtugepaiaSifiicupli
fcigellraoispSpcloexiuiiudistaraic
cgirSdllicsoipcliuispoirataexeufia
tiiuieiclgclSdiaoeplsaorucapsiifxr
pacliiirxslSliiasgieotuufiacdrpeoc
llfscuoiairairlpcopseiSxiiudtgcaie
tipoguaidlecliirosifSaciarpseuicxl
iiaptfusriplosciagcaluexiSicriodel
ioiaxSdlpracluptgcieusrilfiaiicsoe
Saitallirpiudxfusieooigraiseccicpl
opuiocSalscrldriieiisiuetxfgacilpa
aidocalirxitgSieoscliiuiecuarplpfs
prasiriileofxedlosaiacgSipculticui
ipillafcuoslircaeieiiuasitpoScgxrd
ispilpreafitxsuiglcooiuiSeaidrccla
uiipxfSilrsdipoaiirsilaclutecaogec
siaeplipfociSliaierrtlugcdoasxcuii
aisioepieifioxagSsicltcdrcullpiaur
coasSlarlgocauritsilciideuippfiiex
slpeacpicarldegrtxiSiiiisuialooucf
ilpclcsipuuSageaacrfdosiexiriiloit
xrpuoilsfaocaaSeritdlpgsiliuiiecic
ulixlStidaaicsirlsieceougfaciirpop
cgtieloiiSpaaiileaiuduoxcifcslprrs
cruapiorigcaisidpifecelllatusxSiio
liiuSectioixclsioiigpaasclureprfad
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?