替换某些部分以完成完形填空

2024-09-27 21:28:23 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,我有这样一个文本:This is a good reason to stop.(字数总是大于5)

words = ['This', 'is', 'a', 'good', 'reason', 'to', 'stop']
flashcards_count = ciel(len(word) / 3)

我想要的结果是:

[
'This __ a good ______ to ____.'
'____ is a ____ reason __ stop.'
'This is _ good reason to stop.'
]

你可能注意到了,我尽量避免按顺序放空格,除非是最后一张抽认卡。你知道吗

为此,我将words洗牌并将其分块为3,以生成每个抽认卡,但结果可能是这样的:

[
'This is a ____ ______ __ stop.'
'____ __ _ good reason to stop.'
'This is a good reason to ____.'
]

Tags: to文本len顺序iscountthisword
1条回答
网友
1楼 · 发布于 2024-09-27 21:28:23

您可以使用“随机”模块对字符串列表进行无序排列,然后在中间添加一个空格:

import random
list = ['This', 'is', 'a', 'good', 'reason', 'to', 'make', 'stop']
random.shuffle(list)
print " ".join(list)

如果您还想洗牌非字符串,请看一下Shuffling a list of objects。你知道吗

相关问题 更多 >

    热门问题