给定int
的字典,我正在尝试使用每个数字格式化一个字符串,以及该项目的复数形式。
示例输入dict
:
data = {'tree': 1, 'bush': 2, 'flower': 3, 'cactus': 0}
示例输出str
:
'My garden has 1 tree, 2 bushes, 3 flowers, and 0 cacti'
它需要使用任意格式的字符串。
我提出的最佳解决方案是PluralItem
类,用于存储两个属性n
(原始值)和s
(字符串's'
如果是复数,则为空字符串''
,如果没有)。对不同的复数方法进行了分类
class PluralItem(object):
def __init__(self, num):
self.n = num
self._get_s()
def _get_s(self):
self.s = '' if self.n == 1 else 's'
class PluralES(PluralItem):
def _get_s(self):
self.s = 's' if self.n == 1 else 'es'
class PluralI(PluralItem):
def _get_s(self):
self.s = 'us' if self.n == 1 else 'i'
然后通过理解和dict
映射创建一个新的classes
:
classes = {'bush': PluralES, 'cactus': PluralI, None: PluralItem}
plural_data = {key: classes.get(key, classes[None])(value) for key, value in data.items()}
最后,格式字符串和实现:
formatter = 'My garden has {tree.n} tree{tree.s}, {bush.n} bush{bush.s}, {flower.n} flower{flower.s}, and {cactus.n} cact{cactus.s}'
print(formatter.format(**plural_data))
输出以下内容:
My garden has 1 tree, 2 bushes, 3 flowers, and 0 cacti
对于这种毫无疑问的共同需求,我犹豫是否愿意放弃这样一个错综复杂的解决方案。
有没有办法使用内置的format
方法格式化这样的字符串,以及最少的附加代码?伪代码可能类似于:
"{tree} tree{tree(s)}, {bush} bush{bush(es)}, {flower} flower{flower(s)}, {cactus} cact{cactus(i,us)}".format(data)
如果值为复数,则括号返回内容,或者如果内容有逗号,则表示复数/单数
答案 0 :(得分:26)
查看inflect package。它会使事物多元化,并且会做一大堆其他的语言诡计。这些情况有太多特殊情况了!
来自上述链接的文档:
import inflect
p = inflect.engine()
# UNCONDITIONALLY FORM THE PLURAL
print("The plural of ", word, " is ", p.plural(word))
# CONDITIONALLY FORM THE PLURAL
print("I saw", cat_count, p.plural("cat",cat_count))
对于您的具体示例:
{print(str(count) + " " + p.pluralize(string, count)) for string, count in data.items() }
答案 1 :(得分:17)
import string
class PluralFormatter(string.Formatter):
def get_value(self, key, args, kwargs):
if isinstance(key, int):
return args[key]
if key in kwargs:
return kwargs[key]
if '(' in key and key.endswith(')'):
key, rest = key.split('(', 1)
value = kwargs[key]
suffix = rest.rstrip(')').split(',')
if len(suffix) == 1:
suffix.insert(0, '')
return suffix[0] if value <= 1 else suffix[1]
else:
raise KeyError(key)
data = {'tree': 1, 'bush': 2, 'flower': 3, 'cactus': 0}
formatter = PluralFormatter()
fmt = "{tree} tree{tree(s)}, {bush} bush{bush(es)}, {flower} flower{flower(s)}, {cactus} cact{cactus(i,us)}"
print(formatter.format(fmt, **data))
<强>输出:强>
1 tree, 2 bushes, 3 flowers, 0 cacti
<强>更新强>
如果您使用的是Python 3.2+(已添加str.format_map
),您可以使用OP(使用自定义词典)的想法。
class PluralDict(dict):
def __missing__(self, key):
if '(' in key and key.endswith(')'):
key, rest = key.split('(', 1)
value = super().__getitem__(key)
suffix = rest.rstrip(')').split(',')
if len(suffix) == 1:
suffix.insert(0, '')
return suffix[0] if value <= 1 else suffix[1]
raise KeyError(key)
data = PluralDict({'tree': 1, 'bush': 2, 'flower': 3, 'cactus': 0})
fmt = "{tree} tree{tree(s)}, {bush} bush{bush(es)}, {flower} flower{flower(s)}, {cactus} cact{cactus(i,us)}"
print(fmt.format_map(data))
输出:与上述相同。
答案 2 :(得分:3)
如果要复数的单词数量有限,我发现将它们作为列表[singular, plural]
比较容易,然后创建一个小函数以给定数量返回索引:
def sp(num):
if num == 1:
return 0
else:
return 1
然后它是这样的:
lemon = ["lemon", "lemons"]
str = f"Hi I have bought 2 {lemon[sp(2)]}"
实际上,如果您拆分单词,您可以一次获得很多:
s = ["","s"]
str = f"Hi I have 1 cow{s[sp(1)]}"
答案 3 :(得分:2)
如果你碰巧使用Django,很容易:pluralize
是一个函数。
它通常用在模板中:
You have {{ num_messages }} message{{ num_messages|pluralize }}.
但是,您也可以在python代码中使用它:
f'You have {num_messages} message{pluralize(num_messages)}.'
在Python2中,这将是:
'You have {} message{}.'.format(num_messages, pluralize(num_messages))
或:
'You have %d message%s' % (num_messages, pluralize(num_messages))
Django复数文档: https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#pluralize
答案 4 :(得分:1)
我会选择像
这样的东西class Pluralizer:
def __init__(self, value):
self.value = value
def __format__(self, formatter):
formatter = formatter.replace("N", str(self.value))
start, _, suffixes = formatter.partition("/")
singular, _, plural = suffixes.rpartition("/")
return "{}{}".format(start, singular if self.value == 1 else plural)
"There are {:N thing/s} which are made of {:/a cactus/N cacti}".format(Pluralizer(10), Pluralizer(1))
#>>> 'There are 10 things which are made of a cactus'
格式为always/singular/plural
,singular
(然后plural
)可选。
所以
"xyz/foo/bar".format(Pluralizer(1)) == "xyzfoo"
"xyz/foo/bar".format(Pluralizer(2)) == "xyzbar"
"xyz/bar".format(Pluralizer(1)) == "xyz"
"xyz/bar".format(Pluralizer(2)) == "xyzbar"
"xyz".format(Pluralizer(1)) == "xyz"
"xyz".format(Pluralizer(2)) == "xyz"
然后你的例子就是:
data = {'tree': 1, 'bush': 2, 'flower': 3, 'cactus': 0}
string = 'My garden has {tree:N tree/s}, {bush:N bush/es}, {flower:N flower/s}, and {cactus:N cact/us/i}'
string.format_map({k: Pluralizer(v) for k, v in data.items()})
#>>> 'My garden has 1 tree, 2 bushes, 3 flowers, and 0 cacti'
答案 5 :(得分:0)
我受到以上答案的启发,特别是@Veedrac的答案,创建了一个实用程序:
https://gist.github.com/elidchan/40baea13bb91193a326e3a8c4cbcaeb9
功能:
来自文档字符串:
"""
Usage:
>>> from utils.verbiage import Plurality
>>> f"We have {Plurality(0, 'g/oose/eese')}."
'We have 0 geese.'
>>> f"We have {Plurality(1, 'g/oose/eese')}."
'We have 1 goose.'
>>> f"We have {Plurality(2, 'g/oose/eese')}."
'We have 2 geese.'
>>> oxen = Plurality('ox/en')
>>> oxen.template_formatter
'1=$n $thing;n=$n $things'
>>> f"We have {oxen(0)}."
'We have 0 oxen.'
>>> f"We have {oxen(1)}."
'We have 1 ox.'
>>> f"We have {oxen(2)}."
'We have 2 oxen.'
>>> cows = Plurality('/cow/kine', '0=no $things', '1=$a $thing')
>>> cows.template_formatter
'0=no $things;1=a $thing;n=$n $things'
>>> f"We have {cows(0)}."
'We have no kine.'
>>> f"We have {cows(1)}."
'We have a cow.'
>>> f"We have {cows(2)}."
'We have 2 kine.'
>>> 'We have {:0=no $things;0.5=half $a $thing}.'.format(Plurality(0, 'octop/us/odes'))
'We have no octopodes.'
>>> 'We have {:octop/us/odes;0=no $things;0.5=half $a $thing}.'.format(Plurality(0.5))
'We have half an octopus.'
>>> 'We have {:4;octop/us/odes;0=no $things;0.5=half $a $thing}.'.format(Plurality())
'We have 4 octopodes.'
>>> data = {'herb': 1, 'bush': 2, 'flower': 3, 'cactus': 0}
>>> s = "We have {herb:herb/s}, {bush:bush/es}, {flower:flower/s}, and {cactus:cact/us/i}."
>>> s.format_map({k: Plurality(v) for k, v in data.items()})
'We have 1 herb, 2 bushes, 3 flowers, and 0 cacti.'
>>> vague = Plurality('0=no $things;1=$a $thing;2=a couple $things;n=some $things')
>>> s.format_map({k: vague(v) for k, v in data.items()})
'We have an herb, a couple bushes, some flowers, and no cacti.'
"""
答案 6 :(得分:0)
如果只有两种形式,并且只需要快速而肮脏的修复程序,请尝试's'[:i!=1]
:
for i in range(5):
print(f"{i} bottle{'s'[:i!=1]} of beer.")
输出:
0 bottles of beer.
1 bottle of beer.
2 bottles of beer.
3 bottles of beer.
4 bottles of beer.
工作原理:
i
为1,则i != 1
的值为False
({{1}伪装成0
)的bool
。 int
给出空字符串。"s"[:0]
,该数字为"s"[:1]
。我的1美分;)