【发布时间】:2020-10-02 04:50:27
【问题描述】:
我试过了:
text="Sample sentence."
random.shuffle(text)
print(''.join(text))
但这会打乱一切,可能的输出:
nmactnpleSSe ee
我想要这样的东西:
Smplea ntesence.
【问题讨论】:
-
您不能随机播放字符串 - 它们是不可变的。因此显示的输出与您发布的代码不对应。
-
text[0] + ''.join(random.shuffle(list(text[1:-1]))) + text[-1]只要len(text)大于 1。想法是保持 first 和 last 并在其间随机播放 -
@YossiLevi 您的
shuffle返回的不是None? -
正确。这个绝对更好 -
c = list(text[1:-1]);random.shuffle(c);print(text[1] + ''.join(c) + text[-1])。谢谢
标签: python