Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control a print format when printing a list in Python

I have a list with floating point number named a. When I print the list with print a. I get the result as follows.

[8.364, 0.37, 0.09300000000000003, 7.084999999999999, 0.469, 0.303, 9.469999999999999, 0.28600000000000003, 0.2290000000 000001, 9.414, 0.9860000000000001, 0.534, 2.1530000000000005]

Can I tell the list printer to print the format of "5.3f" to get better result?

[8.364, 0.37, 0.093, 7.084, 0.469, 0.303, 9.469, 0.286, 0.229, 9.414, 0.986, 0.534, 2.153]

like image 304
prosseek Avatar asked Sep 08 '11 16:09

prosseek


People also ask

How do you print a list in Python format?

Convert a list to a string for display : If it is a list of strings we can simply join them using join() function, but if the list contains integers then convert it into string and then use join() function to join them to a string and print the string.

How do you print a list in Python nicely?

If you just want to know the best way to print a list in Python, here's the short answer: Pass a list as an input to the print() function in Python. Use the asterisk operator * in front of the list to “unpack” the list into the print function. Use the sep argument to define how to separate two list elements visually.

How do you use %d in print statement in Python?

The %d operator is used as a placeholder to specify integer values, decimals, or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified. Floating-point numbers are converted automatically to decimal values.

How do I print a list of elements separately?

Print list elements on separate Lines using sep # Use the sep argument to print the elements of a list on separate lines, e.g. print(*my_list, sep='\n') . The items in the list will get unpacked in the call to the print() function and will get printed on separate lines.


2 Answers

In [4]: print ['%5.3f' % val for val in l]
['8.364', '0.370', '0.093', '7.085', '0.469', '0.303', '9.470', '0.286', '0.229', '1.000', '9.414', '0.986', '0.534', '2.153']

where l is your list.

edit: If the quotes are an issue, you could use

In [5]: print '[' + ', '.join('%5.3f' % v for v in l) + ']'
[8.364, 0.370, 0.093, 7.085, 0.469, 0.303, 9.470, 0.286, 0.229, 1.000, 9.414, 0.986, 0.534, 2.153]
like image 81
NPE Avatar answered Nov 05 '22 06:11

NPE


If you need this to work in nested structures, you should look into the pprint module. The following should do what you want, in this context:

from pprint import PrettyPrinter
class MyPrettyPrinter(PrettyPrinter):
    def format(self, object, context, maxlevels, level):
        if isinstance(object, float):
            return ('%.2f' % object), True, False
        else:
            return PrettyPrinter.format(self, object, context,
                                        maxlevels, level)
print MyPrettyPrinter().pprint({1: [8, 1./3, [1./7]]})
# displays {1: [8, 0.33, [0.14]]}

For more details on pprint, see: http://docs.python.org/library/pprint.html

If it's just for a flat list, then aix's solution is good. Or if you don't like the extra quotes, you could do:

print '[%s]' % ', '.join('%.3f' % val for val in list)
like image 26
Edward Loper Avatar answered Nov 05 '22 05:11

Edward Loper