简体   繁体   English

用元组切片进行Python字符串插值?

[英]Python string interpolation with tuple slices?

I'm using python2.7 and I was wondering what the reasoning is behind pythons string interpolation with tuples. 我正在使用python2.7,我想知道使用元组进行pythons字符串插值的原因是什么。 I was getting caught up with TypeErrors when doing this small bit of code: 在做这么少的代码时我遇到了TypeErrors

def show(self):
    self.score()
    print "Player has %s and total %d." % (self.player,self.player_total)
    print "Dealer has %s showing." % self.dealer[:2]

Prints: 打印:

Player has ('diamond', 'ten', 'diamond', 'eight') and total 18
Traceback (most recent call last):
File "trial.py", line 43, in <module>
    Blackjack().player_options()
  File "trial.py", line 30, in player_options
    self.show()
  File "trial.py", line 27, in show
    print "Dealer has %s showing." % (self.dealer[:2])
TypeError: not all arguments converted during string formatting

So I found that I needed to change the fourth line where the error was coming from, to this: 所以我发现我需要更改错误来自的第四行,以此:

print "Dealer has %s %s showing." % self.dealer[:2]

With two %s operators, one for each item in the tuple slice. 使用两个%s运算符,一个元组用于元组切片中的每个项目。 When I was checking out what was going on with this line though I added in a print type(self.dealer[:2]) and would get: 当我查看这行时发生了什么,虽然我添加了一个print type(self.dealer[:2])并且会得到:

<type 'tuple'> 

Like I expected, why would a non-sliced tuple like the Player has %s and total %d." % (self.player,self.player_total) format fine and a sliced tuple self.dealer[:2] not? They're both the same type why not pass the slice without explicitly formatting every item in the slice? 就像我预期的那样,为什么像Player has %s and total %d." % (self.player,self.player_total)这样的非切片元组Player has %s and total %d." % (self.player,self.player_total)格式很好,切片元组self.dealer[:2]不是?他们'两个相同的类型为什么不通过切片而不显式格式化切片中的每个项目?

String interpolation requires a format code for each element of the tuple argument. 字符串插值需要元组参数的每个元素的格式代码。 You could instead use the new .format method of strings: 您可以使用新的.format字符串方法:

>>> dealer = ('A','J','K')
>>> print 'Dealer has {} showing'.format(dealer[:2])
Dealer has ('A', 'J') showing

But note that with one argument you get the string representation of the tuple printed, along with parentheses and commas. 但请注意,使用一个参数,您将获得打印元组的字符串表示形式,以及括号和逗号。 You can use tuple unpacking to send the arguments separately, but then you need two format placeholders. 您可以使用元组解包来单独发送参数,但是您需要两个格式占位符。

>>> print 'Dealer has {} {} showing'.format(*dealer[:2])
Dealer has A J showing

As of Python 3.6, there are f-strings. 从Python 3.6开始,有f字符串。 Expressions can be placed into the curly braces: 表达式可以放在花括号中:

>>> dealer = ('A','J','K')
>>> print(f'Dealer has {dealer[0]} {dealer[1]} showing')
Dealer has A J showing

Nothing is wrong with the slice. 切片没有问题。 You would get the same error when passing a tuple literal with incorrect number of elements. 传递元组数量不正确的元组文字时会出现相同的错误。

"Dealer has %s showing." % self.dealer[:2]

is the same as: 是相同的:

"Dealer has %s showing." % (self.dealer[0], self.dealer[1])

Which is obviously an error. 这显然是一个错误。

So, if you would like to format self.dealer[:2] without tuple unpacking: 所以,如果你想在没有元组解包的情况下格式化self.dealer[:2]

"Dealer has %s showing." % (self.dealer[:2],)

Your error is stemming from the fact that in the second formatting operation, you're passing the incorrect number of arguments. 您的错误源于这样的事实:在第二次格式化操作中,您传递的参数数量不正确。

Do

"dealer has %s %s showing" % self.dealer[:2]

or 要么

"dealer has %s showing" % list(self.dealer[:2])

or 要么

"dealer has %s showing" % self.dealer[0] #or self.dealer[1]

It has nothing to do with not using a tuple literal. 它与不使用元组文字无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM