使用数组dict的Python字符串格式

使用数组dict的Python字符串格式,python,string,string-formatting,Python,String,String Formatting,我有一本价值观词典: words = dict( 'hot' = ['hot', 'scalding', 'warm'], 'cold' = ['cold', 'frigid', 'freezing'], ...) 我想用这个同义词表来循环一个字符串列表,用同义词表中的随机条目格式化标记。我不会提前知道关键词是什么 phrases = ['the water is {word.cold}', 'the sun is {word.hot}', ...] formatted = [

我有一本价值观词典:

words = dict( 
  'hot' = ['hot', 'scalding', 'warm'],
  'cold' = ['cold', 'frigid', 'freezing'],
   ...)
我想用这个同义词表来循环一个字符串列表,用同义词表中的随机条目格式化标记。我不会提前知道关键词是什么

phrases = ['the water is {word.cold}', 'the sun is {word.hot}', ...]
formatted = [phrase.format(word=words, somerandomizingfunction) for phrase in phrases]

但这(正如预期的)是将整个数组插入字符串中。有没有办法将
选项
函数传递到
格式
或者我需要编写自己的自定义格式功能,包括单词/键匹配?

从本质上讲,这不是需要的自定义
格式
功能<代码>格式只是获取值(以及可选的各种格式规范)

我建议您定义一个函数,该函数接受源单词并根据您想要的启发返回同义词(可能是列表中的随机元素),然后在调用
format
的内部调用该函数

i、 例如

'the water is {0}'.format(getSynonym('cold'))
根据OP的评论进行编辑:


如果您有动态键,则可以将表示键的变量直接传递到函数中。

它本身不是所需的自定义
格式
功能<代码>格式只是获取值(以及可选的各种格式规范)

我建议您定义一个函数,该函数接受源单词并根据您想要的启发返回同义词(可能是列表中的随机元素),然后在调用
format
的内部调用该函数

i、 例如

'the water is {0}'.format(getSynonym('cold'))
根据OP的评论进行编辑:


如果有动态键,可以将表示键的变量直接传递到函数中。

这种方法如何:

import random

words = dict(hot=['hot', 'scalding', 'warm'],
             cold=['cold', 'frigid', 'freezing'])
演示:


希望可以帮助您。

这种方法如何:

import random

words = dict(hot=['hot', 'scalding', 'warm'],
             cold=['cold', 'frigid', 'freezing'])
演示:


希望可以帮助您。

我相信通过对内置的
dict
类进行子类化,您可以实现您想要的。请参见以下代码的可调试/可分步演示,网址为

import random

class WordDict(dict):
    def __getitem__(self, key):
        vals = dict.__getitem__(self, key)
        return random.choice(vals)

words = WordDict(
    cold = ["cold", "frigid", "freezing"],
    hot = ["scathing", "burning", "hot"]
)

for x in xrange(10):
    print('the water is {word[cold]}'.format(word=words))
import random

class WordDict(dict):
    def __getitem__(self, key):
        vals = dict.__getitem__(self, key)
        return random.choice(vals)

words = WordDict(
    cold = ["cold", "frigid", "freezing"],
    hot = ["scathing", "burning", "hot"]
)

phrases = ['the water is {word[cold]}', 'the sun is {word[hot]}']

for x in xrange(10):
    for phrase in phrases:
        print phrase.format(word=words)
重写
\uuuu getitem\uuuu
方法将允许您假设每个键/值对的每个值将是什么(一个列表),此时您可以从值列表中返回一个随机项

上述代码的输出如下:

the water is freezing
the water is cold
the water is freezing
the water is frigid
the water is cold
the water is frigid
the water is cold
the water is freezing
the water is freezing
the water is freezing
更新

为了确保我的答案与您的问题/请求完全匹配,我调整了上面的代码以包含短语数组。在

import random

class WordDict(dict):
    def __getitem__(self, key):
        vals = dict.__getitem__(self, key)
        return random.choice(vals)

words = WordDict(
    cold = ["cold", "frigid", "freezing"],
    hot = ["scathing", "burning", "hot"]
)

for x in xrange(10):
    print('the water is {word[cold]}'.format(word=words))
import random

class WordDict(dict):
    def __getitem__(self, key):
        vals = dict.__getitem__(self, key)
        return random.choice(vals)

words = WordDict(
    cold = ["cold", "frigid", "freezing"],
    hot = ["scathing", "burning", "hot"]
)

phrases = ['the water is {word[cold]}', 'the sun is {word[hot]}']

for x in xrange(10):
    for phrase in phrases:
        print phrase.format(word=words)
输出:

the water is frigid
the sun is scathing
the water is freezing
the sun is burning
the water is freezing
the sun is hot
the water is cold
the sun is scathing
the water is freezing
the sun is hot
the water is cold
the sun is scathing
the water is frigid
the sun is scathing
the water is frigid
the sun is hot
the water is frigid
the sun is scathing
the water is freezing
the sun is hot

我相信,通过对内置
dict
类进行子类化,您可以实现您想要的。请参见以下代码的可调试/可分步演示,网址为

import random

class WordDict(dict):
    def __getitem__(self, key):
        vals = dict.__getitem__(self, key)
        return random.choice(vals)

words = WordDict(
    cold = ["cold", "frigid", "freezing"],
    hot = ["scathing", "burning", "hot"]
)

for x in xrange(10):
    print('the water is {word[cold]}'.format(word=words))
import random

class WordDict(dict):
    def __getitem__(self, key):
        vals = dict.__getitem__(self, key)
        return random.choice(vals)

words = WordDict(
    cold = ["cold", "frigid", "freezing"],
    hot = ["scathing", "burning", "hot"]
)

phrases = ['the water is {word[cold]}', 'the sun is {word[hot]}']

for x in xrange(10):
    for phrase in phrases:
        print phrase.format(word=words)
重写
\uuuu getitem\uuuu
方法将允许您假设每个键/值对的每个值将是什么(一个列表),此时您可以从值列表中返回一个随机项

上述代码的输出如下:

the water is freezing
the water is cold
the water is freezing
the water is frigid
the water is cold
the water is frigid
the water is cold
the water is freezing
the water is freezing
the water is freezing
更新

为了确保我的答案与您的问题/请求完全匹配,我调整了上面的代码以包含短语数组。在

import random

class WordDict(dict):
    def __getitem__(self, key):
        vals = dict.__getitem__(self, key)
        return random.choice(vals)

words = WordDict(
    cold = ["cold", "frigid", "freezing"],
    hot = ["scathing", "burning", "hot"]
)

for x in xrange(10):
    print('the water is {word[cold]}'.format(word=words))
import random

class WordDict(dict):
    def __getitem__(self, key):
        vals = dict.__getitem__(self, key)
        return random.choice(vals)

words = WordDict(
    cold = ["cold", "frigid", "freezing"],
    hot = ["scathing", "burning", "hot"]
)

phrases = ['the water is {word[cold]}', 'the sun is {word[hot]}']

for x in xrange(10):
    for phrase in phrases:
        print phrase.format(word=words)
输出:

the water is frigid
the sun is scathing
the water is freezing
the sun is burning
the water is freezing
the sun is hot
the water is cold
the sun is scathing
the water is freezing
the sun is hot
the water is cold
the sun is scathing
the water is frigid
the sun is scathing
the water is frigid
the sun is hot
the water is frigid
the sun is scathing
the water is freezing
the sun is hot

这个“选择”功能将如何工作?如果你总是选择第一个词,你会得到完全相同的词,而另一方面,不是所有的词都有同义词。我能想到的唯一选择是使用
random%数组大小
,这将是不一致的(如果这很重要的话)。这个“选择”函数将如何工作?如果你总是选择第一个词,你会得到完全相同的词,而另一方面,不是所有的词都有同义词。我能想到的唯一选择是使用
random%数组大小
,这将是不一致的(如果这很重要的话)。我需要这是动态的-我事先不知道键,所以我不知道要传递给getSynonym的参数。在这种情况下,只需将变量键传递给函数。我需要这是动态的-我事先不知道键,所以我不知道要传递给getSynonym的参数。在这种情况下,只需将变量键传递给函数。谢谢Tok,但这会引入与下面相同的问题。我在一大堆不同的字符串上循环,这些字符串有不同的主题词表键。我不会提前知道“cold”键,需要能够动态地使用该键检索列表,然后随机选择其中一个单词。您可以创建一个函数,定义某个单词出现在同义词库中,然后将该单词用作字典的键。谢谢Tok,但这将引入与下面相同的问题。我在一大堆不同的字符串上循环,这些字符串有不同的主题词表键。我不会提前知道“cold”键,需要能够动态使用该键检索列表,然后随机选择其中一个单词。您可以创建一个函数,定义某个单词出现在同义词库中,然后将该单词用作字典的键。谢谢!这正是我想要的。我本来打算扩展string.Formatter,但这要简单得多。谢谢!这正是我想要的。我打算扩展string.Formatter,但这要简单得多。