-3

I am opening this thread because I would like to know how and if it is possible to customize the list output like this:

1       2.33     KG
2       3.0      KG
3       (empty)   3
x       222      KG
y       233.4    KG
y       112      %
z       222      KG
w       9.98     KG
a       3224     KG
...     ....    ....

Could someone please let me know?

Soichiro
  • 81
  • 2
  • 9
  • 1
    What does your list output look like currently? – ILostMySpoon Jun 09 '17 at 11:39
  • https://stackoverflow.com/questions/6200288/pretty-printing-a-list-in-a-tabular-format , https://stackoverflow.com/questions/1396820/apt-like-column-output-python-Library , https://stackoverflow.com/questions/23415357/python-pretty-printing-a-list-in-a-tabular-format?noredirect=1&lq=1 the list of duplicate is long – Drag and Drop Jun 09 '17 at 11:41
  • 2
    In fact this question is not clear enought to be a clear dupe, has we have no idea what input looks like. – Drag and Drop Jun 09 '17 at 11:43
  • @DragandDrop or current output – WhatsThePoint Jun 09 '17 at 11:46
  • Possible duplicate of [Create nice column output in python](https://stackoverflow.com/questions/9989334/create-nice-column-output-in-python) – Drag and Drop Jun 09 '17 at 11:49

3 Answers3

1

Depending on what you are trying to do you could try out either pandas, which is great at handling and printing 2d arrays, or string formatting discussed in this answer

0

Try to construct a list inside a list. like [[1 ,2.33 ,KG],[2, 3.0,KG],[3,' ',3]] Then you can access each element by iterating over the lists.

<table>    
for i in [[1 ,2.33 ,KG],[2, 3.0,KG],[3,' ',3]]:
    <tr>
    for j in i:
       <td>j</td> 
    </tr>
 </table>
Mallik Sai
  • 186
  • 1
  • 4
  • 16
0

Please show your code. But I assume you want to know how to do what you've typed above.

Preferably, you'd want to place the variables in a list of tuples like this:

your_tuple_list_name = [(1, 2.33, 'KG'), (2, 3.0, 'KG'), (3, '(empty)', 'KG'), ('x', 222, 'KG'), ('y', 233.4, 'KG'), ('y', 112, '%'), ('z', 222, 'KG'), ('w', 9.98, 'KG'), ('a', 3224, 'KG'), ('.', '....', '..'), (add other tuples here)]`

If you want to add more items to the list of tuples just add them following the format:

(first_column, second_column, third_column)

And then the method would be like this:

def formatted_printing(your_tuple_list):
    space = ' '
    for n in range(len(your_tuple_list)):
        number = 8 - len(str((your_tuple_list[n][1])))
        print("{:>}    {:<}{}{:<}".format(your_tuple_list[n][0], your_tuple_list[n][1], (space * number), your_tuple_list[n][2]))


formatted_printing(your_tuple_list_name)

With the following being the output:

1    2.33    KG
2    3.0     KG
3    (empty) KG
x    222     KG
y    233.4   KG
y    112     %
z    222     KG
w    9.98    KG
a    3224    KG
.    ....    ....
loadingaccount
  • 58
  • 1
  • 1
  • 5