How to print python nested list in columns

I have a program that creates a python list as its output. A list is a nested list: a list of the list [name, address, phone number] that I want to print in column format. It seems that the question was a very simple idea, but I could not find an easy way to extract the data from the list. If I type (list), I get something like this: ['name', 'address', 'number number'], etc. For each item in the list. I am using Python 3 on a Windows platform. Note. I am not an OOP programmer (at the moment).

Regards Bill

+4
source share
5 answers

Go to the list as follows:

for name,add,num in lis: print (name,add,num) 

Demo:

 >>> lis = [['name','address','phone number']] >>> for name,add,num in lis: ... print (name,add,num) ... name address phone number 

You can also use string formatting for better output:

 >>> lis = [['name','address','phone number']] >>> for name,add,num in lis: print ("{:<10}{:^20}{:^10}".format(name,add,num)) ... name address phone number 
+2
source

prettytable can create very nice ASCII tables. Example from tutorial :

 from prettytable import PrettyTable x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"]) x.align["City name"] = "l" # Left align city names x.add_row(["Adelaide",1295, 1158259, 600.5]) x.add_row(["Brisbane",5905, 1857594, 1146.4]) x.add_row(["Darwin", 112, 120900, 1714.7]) x.add_row(["Hobart", 1357, 205556, 619.5]) x.add_row(["Sydney", 2058, 4336374, 1214.8]) x.add_row(["Melbourne", 1566, 3806092, 646.9]) x.add_row(["Perth", 5386, 1554769, 869.4]) print x 

Gotta print something like this

 +-----------+------+------------+-----------------+ | City name | Area | Population | Annual Rainfall | +-----------+------+------------+-----------------+ | Adelaide | 1295 | 1158259 | 600.5 | | Brisbane | 5905 | 1857594 | 1146.4 | | Darwin | 112 | 120900 | 1714.7 | | Hobart | 1357 | 205556 | 619.5 | | Sydney | 2058 | 4336374 | 1214.8 | | Melbourne | 1566 | 3806092 | 646.9 | | Perth | 5386 | 1554769 | 869.4 | +-----------+------+------------+-----------------+ 

Adapting this example for your use case should be trivial.

+2
source

You can use the print operator, for example, if you want all fields to be 20 characters wide:

 for e in list: name, address, phone = e print "%20s %20s %20s" % (name, address, phone) 
+1
source
 for name, address, phone_number in a_list: print '{}\t{}\t{}'.format(name, address, phone_number) 
0
source

Other answers will be truncated by your entries if they fit the size of the field. If you want them converted instead, you need to use the textwrap module.

 import textwrap import itertools col_width = 20 header = ["Name", "Address", "Phone Number"] def columnar_record(record): columns = (textwrap.wrap(item, col_width) for item in record) line_tuples = itertools.zip_longest(*columns, fillvalue="") lines = ("".join(item.ljust(col_width) for item in line_tuple) for line_tuple in line_tuples) return "\n".join(lines) def print_columns(records): print(columnar_record(header)) for record in records: print(columnar_record(record)) a = ["Bill Bradley", "123 North Main St., Anytown, NY 12345", "800-867-5309"] b = ["John Doe", "800 South Main Street, Othertown, CA 95112", "510-555-5555"] print_columns([a, b]) 
0
source

Source: https://habr.com/ru/post/1490155/


All Articles