我是Python的新手,所以我不确定这个操作究竟是什么,因此我很难在其中搜索信息。
基本上我想要一个字符串,例如:
"[[size]] widget that [[verb]] [[noun]]"
大小,动词和名词都是列表。
我想将字符串解释为元语言,这样我就可以从列表中排出很多句子。作为元语言,我还可以使用其他字符串来使用这些预定义列表来生成更多排列。
在Python中是否有像这样的变量替换功能?如果我应该只使用谷歌这个术语用什么术语来描述呢?
答案 0 :(得分:4)
如果您将语法更改为
"{size} widget that {verb} {noun}"
然后你可以使用字符串的format
方法来进行替换:
"{size} widget that {verb} {noun}".format(size='Tiny',verb='pounds',noun='nails')
或
choice={'size':'Big',
'verb':'plugs',
'noun':'holes'}
"{size} widget that {verb} {noun}".format(**choice)
答案 1 :(得分:2)
如果您有sizes
,verbes
,nounes
列表,则可以使用以下一种方法:
import itertools, string
t = string.Template("$size widget that $verb $noun")
for size, verb, noun in itertools.product(sizes, verbes, nounes):
print t.safe_substitute(size=size, verb=verb, noun=noun)
答案 2 :(得分:1)
您希望将re.sub()
或其正则表达式对象等效方法与回调函数一起使用。
答案 3 :(得分:1)
试试这个脚本:
import random #just needed for the example, not the technique itself
import re # regular expression module for Python
template = '[[size]] widget that [[verb]] [[noun]]'
p = re.compile('(\[\[([a-z]+)\]\])') # match placeholder and the word inside
matches = p.findall(template) # find all matches in template as a list
#example values to show you can do substitution
values = {
'size': ('tiny', 'small', 'large'),
'verb': ('jumps', 'throws', 'raises'),
'noun': ('shark', 'ball', 'roof')
}
print 'After each sentence is printed, hit Enter to continue or Ctrl-C to stop.'
while True: # forever
s = template
#this loop replaces each placeholder [[word]] with random value based on word
for placeholder, key in matches:
s = s.replace(placeholder, random.choice(values[key]))
print s
try:
raw_input('') # pause for input
except KeyboardInterrupt: #Ctrl-C
break # out of loop
示例输出:
large widget that jumps ball
small widget that raises ball
small widget that raises ball
large widget that jumps ball
small widget that raises ball
tiny widget that raises shark
small widget that jumps ball
tiny widget that raises shark
答案 4 :(得分:0)
正则表达式过度。然后使用循环设置大小动词和名词变量:
print("%(size)s widget that %(verb)s %(noun)s" % {"size":size, "verb":verb, "noun":noun})