用用户输入

时间:2016-08-22 22:01:06

标签: python

所以,我正在为madlibs制作一个Python 3小程序。到目前为止,我有它,所以它读取一个文本文件,从中提取一个随机的故事,并将该故事分成一个列表,以便通过它进行搜索。一切都很好。我将这些故事格式化,以便在需要单词时使用(名词),(副词),(名称)等等。但是,我在替换这些字符串时遇到了一些问题。这是一个例子:

>>> for i in range(0,len(poss)):
...  if '(' in poss[i]: poss[i] = input('{0}: '.format(poss[i].replace('(','').replace(')','')))
...
noun: monster
name.: Strange
name: Strange
adjective: yellow
noun!: eatery
place.
: Times Square
>>> poss = ' '.join(poss)
>>> print(poss)
                               Back to the Future

Marty was an innocent, young monster and made friends with the local scientist,
Doc Strange Doc Strange was a bit off, but he was one yellow genius. One day,
he told Marty that he had an invented a eatery Of course, Marty had to see it
in action. Late that night, they met at Times Square

它搜索每个对象中的'('字符,然后用单词替换整个对象。它不会保留可能涉及的标点符号。此外,你可以看到标点符/换行符留在调用input()时显示。如何有效地替换括号中包含的子串?

供参考,以下是从文件中提取的原始文本:

                               Back to the Future

Marty was an innocent, young (noun) and made friends with the local scientist,
Doc (name). Doc (name) was a bit off, but he was one (adjective) genius. One day,
he told Marty that he had an invented a (noun)! Of course, Marty had to see it
in action. Late that night, they met at (place).

我的预期结果是:

                               Back to the Future

Marty was an innocent, young monster and made friends with the local scientist,
Doc Strange. Doc Strange was a bit off, but he was one yellow genius. One day,
he told Marty that he had an invented a eatery! Of course, Marty had to see it
in action. Late that night, they met at Times Square.

3 个答案:

答案 0 :(得分:2)

要解决您的方法存在的问题,我需要查看完整的代码,但我还有其他建议。

你实际上可以在这里使用正则表达式,这允许在一行(几乎)中进行替换。

In [1]: import re

In [2]: story = '''                               Back to the Future
   ...: 
   ...: Marty was an innocent, young (noun) and made friends with the local scientist,
   ...: Doc (name). Doc (name) was a bit off, but he was one (adjective) genius. One day,
   ...: he told Marty that he had an invented a (noun)! Of course, Marty had to see it
   ...: in action. Late that night, they met at (place).'''

In [3]: def replace(match):
   ...:     return input('{}: '.format(match.group()))
   ...: 

In [4]: print(re.sub('\((noun|name|adjective|place)\)', replace, story))
(noun): monster
(name): Strange
(name): Strange
(adjective): yellow
(noun): eatery
(place): Times Square
                               Back to the Future

Marty was an innocent, young monster and made friends with the local scientist,
Doc Strange. Doc Strange was a bit off, but he was one yellow genius. One day,
he told Marty that he had an invented a eatery! Of course, Marty had to see it
in action. Late that night, they met at Times Square.

re.sub()接受一个可调用的替换,我们用它来调用input()并从匹配中提取提示。

编辑以匹配括号中的任何词组,您只需更改模式,例如:

print(re.sub('\(([^()]*)\)', replace, story))

答案 1 :(得分:0)

text = """Marty was an innocent, young (noun) and made friends with the local scientist,
Doc (name). Doc (name) was a bit off, but he was one (adjective) genius. One day,
he told Marty that he had an invented a (noun)! Of course, Marty had to see it
in action. Late that night, they met at (place)."""


match = ['monster', 'Strange', 'Strange', 'yellow', 'eatery', 'Times Square']

splitted = text.split()

for i, item in enumerate(splitted):
    if '(' in item:
        matched = match.pop(0)
        if not item.endswith(')'):
            matched = '{}{}'.format(matched, item[:-1])
        splitted[i] = matched

print ' '.join(splitted)

答案 2 :(得分:0)

好的,为了解决我只想要一次询问每个相同项目的问题,我不得不重写一点。我还在使用@Lex提供的正则表达式,所以感谢他。但是,我不再使用他的re.sub()方法。

import re
l1 = re.findall('\(([^()]*)\)',story)
l2 = []
for i in l1:
 if i not in l2:
  l2.append(i)
for i in l2:
 substr = '({})'.format(i)
 word = input('{}: '.format(i))
 story = story.replace(substr,word)


print(story)

findall()函数在括号"之间找到与"的正则表达式匹配的所有实例。并构建一个初始列表。然后,我遍历该列表并将每个唯一标记放入新列表中以消除重复。我遍历新的令牌列表,并用提示的输入替换每个匹配。这正是我想要的applet,因为它消除了重复的提示。

相关问题