Python 类型错误:传递给 list.__format__ 的格式字符串不受支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52374104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:05:58  来源:igfitidea点击:

TypeError: unsupported format string passed to list.__format__

python

提问by rabin senchuri

I have two list named results and p_results. I want to show those list values in table like if

我有两个名为 results 和 p_results 的列表。我想在表中显示这些列表值,如

results = [1,2,3,4]
p_results = [5,6,7,8]

I want something like this

我想要这样的东西

1     5
2     6
3     7
4     8

        print('{:3}{:20}'.format(results, p_results))

running the code:

运行代码:

runfile('D:/4/2d.py', wdir='D:/4')
Traceback (most recent call last):

  File "<ipython-input-59-1abb0c96f0c0>", line 1, in <module>
    runfile('D:/4/2d.py', wdir='D:/4')

  File "C:\Users\Rabinsen\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\Users\Rabinsen\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/4/2d.py", line 61, in <module>
    print('{:3}{:20}'.format(results, p_results))

TypeError: unsupported format string passed to list.__format__

回答by Jean-Fran?ois Fabre

You can pass a listtype to format(using just {}), but the formatting you requested isn't available. And the standard formatting isn't suited anyway, so...

您可以将list类型传递给format(仅使用{}),但您请求的格式不可用。无论如何,标准格式都不适合,所以......

What you want is each list on one separate column. You'll have to ziplists together, and iterate on the result to pass it to format:

你想要的是一个单独的列上的每个列表。您必须将zip列表放在一起,并迭代结果以将其传递给格式:

results = [1,2,3,4]
p_results = [5,6,7,8]

for result,p_result in zip(results,p_results):
    print('{:3}{:20}'.format(result,p_result))

That prints something like:

这会打印如下内容:

  1                   5
  2                   6
  3                   7
  4                   8

回答by Istvan

One trick is to solve this:

一个技巧是解决这个问题:

'{:15}'.format('{}'.format([1,2,3]))

回答by LeninGF

I would recommend that you use Pandas library for that purpose

我建议您为此目的使用 Pandas 库

import pandas as pd
results = [1,2,3,4]
p_results = [5,6,7,8]
d = {'column-1':results, 'column-2':p_results}
data_frame = pd.DataFrame(data=d)
print(data_frame)
   column-1  column-2
0         1         5
1         2         6
2         3         7
3         4         8

Notice that you have an index using data frame object

请注意,您有一个使用数据框对象的索引

Also you can make new columns an operations, like for instance let us create a third column to store the product of column-1 and column-2

您也可以使新列成为一项操作,例如让我们创建第三列来存储第 1 列和第 2 列的乘积

data_frame['product'] = data_frame['column-1']*data_frame['column-2']

The result you get now printing is:

您现在打印的结果是:

   column-1  column-2  product
0         1         5        5
1         2         6       12
2         3         7       21
3         4         8       32

Hope this helps! Happy coding!

希望这可以帮助!快乐编码!

回答by vash_the_stampede

Did you try print('{},{}'.format(results[:3], p_results[:20]))

你试过了吗 print('{},{}'.format(results[:3], p_results[:20]))

The way you structured your printlike looks like a combination of two styles,

你构建你print喜欢的方式看起来像是两种风格的组合,

print(f'{results[:3]}, {p_results[:20]}')

print(f'{results[:3]}, {p_results[:20]}')

and

print('{}, {}'.format(results[:3], p_results[:20]))

print('{}, {}'.format(results[:3], p_results[:20]))

Your attempt is

你的尝试是

print('{:3}, {:20}'.format(results, p_results))

print('{:3}, {:20}'.format(results, p_results))

This is the root of your error, here in example reproducing same error:

这是错误的根源,在示例中重现相同的错误:

>>> a = [1, 2, 3]
>>> print('{:1}'.format(a))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported format string passed to list.__format__

>>> print('{}'.format(a[:1]))
[1]