如何实现条件字符串格式?

时间:2022-10-29 20:25:04

I've been working on a text-based game in Python, and I've come across an instance where I want to format a string differently based on a set of conditions.

我一直在用Python编写基于文本的游戏,我遇到过一个实例,我希望根据一组条件对字符串进行不同的格式化。

Specifically, I want to display text describing items in a room. I want this to be displayed, in the room's description, if and only if the item object in question is in the room object's list of items. The way it is set up, I feel that simply concatenating strings based on conditionals will not output as I want, and it would be better to have a different string for each case.

具体来说,我想显示描述房间内物品的文字。我希望在房间的描述中显示此内容,当且仅当有问题的项目对象位于房间对象的项目列表中时。它的设置方式,我觉得简单地连接基于条件的字符串将不会按我的意愿输出,并且最好为每种情况设置不同的字符串。

My question is, is there any pythonic method for formatting strings based on the result of a Boolean conditional? I could use a for loop structure, but I was wondering if there was something easier, similar to a generator expression.

我的问题是,是否有任何pythonic方法根据布尔条件的结果格式化字符串?我可以使用for循环结构,但我想知道是否有更简单的东西,类似于生成器表达式。

I'm looking for something similar to this, in string form

我正在寻找类似于此的东西,以字符串形式

num = [x for x in xrange(1,100) if x % 10 == 0]

As a general example of what I mean:

作为我的意思的一般例子:

print "At least, that's what %s told me." %("he" if gender == "male", else: "she")

I realize that this example is not valid Python, but it shows, in general, what I'm looking for. I'm wondering if there is any valid expression for boolean string formatting, similar to the above. After searching around a bit, I was unable to find anything pertaining specifically to conditional string formatting. I did find several posts on format strings in general, but that is not what I'm looking for.

我意识到这个例子不是有效的Python,但它总体上显示了我正在寻找的东西。我想知道布尔字符串格式是否有任何有效表达式,类似于上面的。在搜索了一下之后,我无法找到任何与条件字符串格式有关的内容。我确实在格式字符串上找到了几个帖子,但这不是我想要的。

If something like that does indeed exist, it would be very useful. I'm also open to any alternate methods that may be suggested. Thanks in advance for any help you can provide.

如果这样的确存在,那将非常有用。我也对可能提出的任何替代方法持开放态度。提前感谢您提供的任何帮助。

2 个解决方案

#1


73  

Your code actually is valid Python if you remove two characters, the comma and the colon.

如果你删除两个字符,逗号和冒号,你的代码实际上是有效的Python。

>>> gender= "male"
>>> print "At least, that's what %s told me." %("he" if gender == "male" else "she")
At least, that's what he told me.

More modern style uses .format, though:

更现代的风格使用.format,但是:

>>> s = "At least, that's what {pronoun} told me.".format(pronoun="he" if gender == "male" else "she")
>>> s
"At least, that's what he told me."

where the argument to format can be a dict you build in whatever complexity you like.

格式化的参数可以是一个dict,你可以用你喜欢的任何复杂性来构建。

#2


8  

There is a conditional expression in Python which takes the form

Python中有一个条件表达式,它采用了这种形式

A if condition else B

Your example can easily be turned into valid Python by omitting just two characters:

通过省略两个字符,可以轻松地将您的示例转换为有效的Python:

print ("At least, that's what %s told me." % 
       ("he" if gender == "male" else "she"))

An alternative I'd often prefer is to use a dictionary:

我经常喜欢的另一种选择是使用字典:

pronouns = {"female": "she", "male": "he"}
print "At least, that's what %s told me." % pronouns[gender]

#1


73  

Your code actually is valid Python if you remove two characters, the comma and the colon.

如果你删除两个字符,逗号和冒号,你的代码实际上是有效的Python。

>>> gender= "male"
>>> print "At least, that's what %s told me." %("he" if gender == "male" else "she")
At least, that's what he told me.

More modern style uses .format, though:

更现代的风格使用.format,但是:

>>> s = "At least, that's what {pronoun} told me.".format(pronoun="he" if gender == "male" else "she")
>>> s
"At least, that's what he told me."

where the argument to format can be a dict you build in whatever complexity you like.

格式化的参数可以是一个dict,你可以用你喜欢的任何复杂性来构建。

#2


8  

There is a conditional expression in Python which takes the form

Python中有一个条件表达式,它采用了这种形式

A if condition else B

Your example can easily be turned into valid Python by omitting just two characters:

通过省略两个字符,可以轻松地将您的示例转换为有效的Python:

print ("At least, that's what %s told me." % 
       ("he" if gender == "male" else "she"))

An alternative I'd often prefer is to use a dictionary:

我经常喜欢的另一种选择是使用字典:

pronouns = {"female": "she", "male": "he"}
print "At least, that's what %s told me." % pronouns[gender]