-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?

5

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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
.    ....    ....

1 Comment

Thank you so much, it really helped me a lot ^_^
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>

2 Comments

Hello Everyone, I was able to do solve my issues with lobfil's solution. Thank you again guys ^_^
@Soichiro if found any author's answer as useful give a tick mark near the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.