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__
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With