简体   繁体   English

如何在列中打印嵌套的python列表

[英]How to print a nested python list in columns

I have a program that produces a python list as its output the list is a nested list: A list of the list [name, address, phone number] which I wish to be able to print in a columnar format. 我有一个生成python列表的程序,它的输出是嵌套列表:我希望能够以列格式打印的列表的列表[名称,地址,电话号码]。 It seems upon stating the question to be a very simple Idea, but I have been unable to find a simple way to extract the data from the list. 似乎在说明问题时这是一个非常简单的想法,但是我一直无法找到一种简单的方法来从列表中提取数据。 If I print(list), I get something like this: ['name','address','phone number'], etc. for each item in the list. 如果我打印(列表),则列表中的每个项目都会得到类似的内容:['name','address','phone number']等。 I'm using Python 3 on a windows platform. 我在Windows平台上使用Python 3。 Note: I am not a OOP programmer(at this point) 注意:我不是OOP程序员(目前)

Regards Bill 关于条例草案

Iterate over the list like this: 像这样遍历列表:

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 a better looking output: 您还可以使用字符串格式来获得更好的外观输出:

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

prettytable can produce very nice looking ASCII tables. prettytable可以产生非常漂亮的ASCII表。 Example from the 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

Should 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. 使该示例适合您的用例应该很简单。

for name, address, phone_number in a_list:
    print '{}\t{}\t{}'.format(name, address, phone_number)

You can use the print statement, for instance if you want all you fields to be 20 characters wide: 您可以使用print语句,例如,如果您希望所有字段都为20个字符宽:

for e in list:
    name, address, phone = e
    print "%20s %20s %20s" % (name, address, phone)

The other answers given will truncate your records if they go over the field size. 如果给出的其他答案超出字段大小,则会截断您的记录。 If you want them to wrap instead, you need to use the textwrap module. 如果要换行,则需要使用textwrap模块。

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM