English 中文(简体)
为什么分配到印刷格式而不是工作?
原标题:Why does assignment of split to print formatting not work?

I am puzzled why I can assign/unpack the return of split() to the appropriate number of variables, but the same fails for use in a print string using formatting directives.

例如,考虑到:

In [202]: s
Out[202]:  here are 4 values 

In [203]: s.split()
Out[203]: [ here ,  are ,  4 ,  values ]

这项工程按预期进行:

In [204]: a, b, c, d = s.split()

In [205]: print  %s %s %s %s  % (a, b, c, d)
here are 4 values

但这失败了。

 In [206]: print  %s %s %s %s  % (s.split())

I am not sure why? Shouldn t the return of split() be unpacked and be distributed over the expected arguments for the formatting strings?

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:laDesktop<ipython-input-206-600f130ff0b2> in <module>()
----> 1 print  %s %s %s %s  % (s.split())

TypeError: not enough arguments for format string

“说明有足够的论据”。 我确实有名单上的项目数目。 由于某种原因,该清单是否没有包装,而是没有转让变数?

I came across this in attempting to answer this question writing column entry just one below another in python

最佳回答

页: 1

>>> s =  here are 4 values 
>>>  %s %s %s %s  % tuple(s.split())
 here are 4 values 

http://docs.python.org/library/string.html#format-string-syntax” rel=“nofollow”>code>.format(, 相反,没有包装这些论点。

 {0} {1} {2} {3} .format(*s.split())
问题回答

The fundamental problem is that %-formatting and unpacking aren t related at all. The % requires that the values be in a tuple; other kinds of sequences won t work. (It will accept a single non-tuple value though.)

这产生了不幸的后果,即所有标记都被视为价值之 t,无论是否可取。 a. 治疗 a 价值是,你必须用另一图:

>>>  %s  % ( a ,  b )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>  %s  % (( a ,  b ),)
"( a ,  b )"

使用<条码>格式更为可取。

我认为,问题在于,有 %的字塔允许提出不包装的论点。 这里使用的方法是:

>>> s =  Here are some values 
>>>  {} {} {} {} .format(*s.split())
 Here are some values 

@senderle指出,这一辛迪加只为第2.7页而工作,因此,这里的编码将与以前的版本合作:

>>> s =  Here are some values 
>>>  {0} {1} {2} {3} .format(*s.split())
 Here are some values 
>>> s =  here are 4 values 
>>> print  %s %s %s %s  % tuple(s.split())
here are 4 values
>>> print  %s  % s.split()
[ here ,  are ,  4 ,  values ]

灰色正试图改变名单,以扼杀它只需要1个参数。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签