Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: unsupported format string passed to list.__format__

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

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__
like image 285
rabin senchuri Avatar asked Sep 17 '18 18:09

rabin senchuri


2 Answers

One trick is to solve the spacing part:

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

For iterating over two lists:

[print('res:', i, ': p_res', j) for i, j in zip(results, p_results)]
res: 1 : p_res 5
res: 2 : p_res 6
res: 3 : p_res 7
res: 4 : p_res 8

Combining formatting and zipping gives you what you wanted to achieve.

like image 79
Istvan Avatar answered Nov 07 '22 23:11

Istvan


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

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

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
like image 4
Jean-François Fabre Avatar answered Nov 07 '22 21:11

Jean-François Fabre