python - How to display a 2D list in this form? -
how can convert 2d list this?
lista=[[1, 2, 3],[3, 4, 5],[8, 5, 9]] listb=[[1 2 3] [3 4 5] [8 5 9]]
as pointed out, have same representation of 1 list in python, list list conversion doesn't make sense. however, if want list string conversion - ways output list nice string, have option of pprint
, or pretty-printing. module standard library seems pretty close you're looking for:
import pprint l = [[1, 2, 3],[3, 4, 5],[8, 5, 9]] pprint.pprint(l, width=12)
gives output
[[1, 2, 3], [3, 4, 5], [8, 5, 9]]
now i've had artificially reduce maximum width bit lists short pprint
, default, not consider them worth shortening.
also, if want string representation in program, can use pprint.pformat
in pretty same way, except returns string:
import pprint l = [[1, 2, 3],[3, 4, 5],[8, 5, 9]] s = pprint.pformat(l, width=12)
Comments
Post a Comment