Python Forum

Full Version: Space between list and column alignment
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can anyone tell me how I can put more than one list in table style display please? With columns and rows are nicely aligned.

Thanks.
Look back at your older Thread🧐
(Mar-09-2021, 11:28 AM)snippsat Wrote: [ -> ]Look back at your older Thread🧐

However, my previous inquiry was for multi dimensional list.

This is time is separate list I am trying to figure out.
Show example of input and wanted output.
1: One way to display a data frame in the form of a table is by using the display() function of IPython.display.

filter_none
brightness_4
# importing the modules
from IPython.display import display
import pandas as PD

# creating a DataFrame
dict = {'Name' : ['Martha', 'Tim', 'Rob', 'Georgia'],
'Maths' : [87, 91, 97, 95],
'Science' : [83, 99, 84, 76]}
df = pd.DataFrame(dict)

# displaying the DataFrame
display(df)

2: In this example we’ll use DataFrame.style. It returns a Styler object, which has useful methods for formatting and displaying DataFrames.

filter_none
brightness_4
# importing the module
import pandas as pd

# creating a DataFrame
dict = {'Name' : ['Martha', 'Tim', 'Rob', 'Georgia'],
'Maths' : [87, 91, 97, 95],
'Science' : [83, 99, 84, 76]}
df = pd.DataFrame(dict)

# displaying the DataFrame
df.style
(Mar-09-2021, 01:53 PM)snippsat Wrote: [ -> ]Show example of input and wanted output.

list_a=[1,2,3,4,5]
list_b=['d','h','f','s','g']
 
 
print(list_a)
print(list_b)
This is the output:
[1, 2, 3, 4, 5]
['d', 'h', 'f', 's', 'g']
Expected output:
[1, 2, 3, 4, 5 ]
['d', 'h', 'f', 's', 'g']
(Mar-15-2021, 01:48 PM)rturus Wrote: [ -> ]
(Mar-09-2021, 01:53 PM)snippsat Wrote: [ -> ]Show example of input and wanted output.

list_a=[1,2,3,4,5]
list_b=['d','h','f','s','g']
 
 
print(list_a)
print(list_b)
This is the output:
[1, 2, 3, 4, 5]
['d', 'h', 'f', 's', 'g']
Expected output:
[1,     2,    3,    4,   5 ]
['d',  'h',  'f',  's', 'g']
List is a data structure(internal use) and not meant to view like this,as that make no senseπŸ¦„
For view take elements out data structure,then can figure a different way to view it.
>>> list_a = [1,2,3,4,5]
>>> list_b = ['d','h','f','s','g']
>>> lst = []
>>> lst.append([str(i) for i in list_a])
>>> lst.append(list_b)
>>> 
>>> lst
[['1', '2', '3', '4', '5'], ['d', 'h', 'f', 's', 'g']]
>>> 
>>> for inner in lst:
...     print(' | '.join((f"{word:4}" for word in inner)))
...     
1    | 2    | 3    | 4    | 5   
d    | h    | f    | s    | g 
>>> from tabulate import tabulate
>>> 
>>> print(tabulate(lst, tablefmt="fancy_grid"))
╒═══╀═══╀═══╀═══╀═══╕
β”‚ 1 β”‚ 2 β”‚ 3 β”‚ 4 β”‚ 5 β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚ d β”‚ h β”‚ f β”‚ s β”‚ g β”‚
β•˜β•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•›
>>> print(tabulate(lst, tablefmt="pretty"))
+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 |
| d | h | f | s | g |
+---+---+---+---+---+
>>> print(tabulate(lst, tablefmt="simple"))
-  -  -  -  -
1  2  3  4  5
d  h  f  s  g
-  -  -  -  -
>>> print(tabulate(lst, tablefmt="youtrack"))
|  1  |  2  |  3  |  4  |  5  |
|  d  |  h  |  f  |  s  |  g  |  
(Mar-15-2021, 08:21 PM)snippsat Wrote: [ -> ]List is a data structure(internal use) and not meant to view like this,as that make no senseπŸ¦„
For view take elements out data structure,then can figure a different way to view it.
>>> list_a = [1,2,3,4,5]
>>> list_b = ['d','h','f','s','g']
>>> lst = []
>>> lst.append([str(i) for i in list_a])
>>> lst.append(list_b)
>>> 
>>> lst
[['1', '2', '3', '4', '5'], ['d', 'h', 'f', 's', 'g']]
>>> 
>>> for inner in lst:
...     print(' | '.join((f"{word:4}" for word in inner)))
...     
1    | 2    | 3    | 4    | 5   
d    | h    | f    | s    | g 
>>> from tabulate import tabulate
>>> 
>>> print(tabulate(lst, tablefmt="fancy_grid"))
╒═══╀═══╀═══╀═══╀═══╕
β”‚ 1 β”‚ 2 β”‚ 3 β”‚ 4 β”‚ 5 β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚ d β”‚ h β”‚ f β”‚ s β”‚ g β”‚
β•˜β•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•§β•β•β•β•›
>>> print(tabulate(lst, tablefmt="pretty"))
+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 |
| d | h | f | s | g |
+---+---+---+---+---+
>>> print(tabulate(lst, tablefmt="simple"))
-  -  -  -  -
1  2  3  4  5
d  h  f  s  g
-  -  -  -  -
>>> print(tabulate(lst, tablefmt="youtrack"))
|  1  |  2  |  3  |  4  |  5  |
|  d  |  h  |  f  |  s  |  g  |  

Thank you. This is very helpful.