简体   繁体   English

带有列表的Python打印列表

[英]Python printing lists with tabulate

I'm trying to print the output of an astronomy simulation so that it looks nice in my console. 我正在尝试打印天文学模拟的输出,以便在控制台中看起来不错。 I generate 4 numpy arrays called Amplitude, Mass, Period and Eccentricity and I want to put them in a table. 我生成了4个numpy数组,分别称为Amplitude,Mass,Period和Eccentricity,我想将它们放在一个表中。 The first index of each array are the values for planet 1, the second for planet 2 etc. 每个数组的第一个索引是行星1的值,第二个索引是行星2的值,依此类推。

So my arrays look like (values are all float numbers, eg 'a1' just a placeholder): 所以我的数组看起来像(值都是浮点数,例如'a1'只是一个占位符):

amp = [a1 a2 a3 a4]
mass = [m1 m2 m3 m4]
period = [p1 p2 p3 p4]
ecc = [e1 e2 e3 e4]

I'd like my table to look like: 我希望桌子看起来像:

planet|amp|mass|period|ecc
1     |a1 |m1  |p1    |e1
2     |a2 |m2  |p2    |e2
...

I've tried using tabulate and something like: 我试过使用制表和类似的东西:

print tabulate(['1', amp[0], mass[0], period[0], ecc[0]], headers=[...])

but I get an error of 'numpy.float64' object is not iterable 但是我得到一个错误的'numpy.float64'对象是不可迭代的

Any help would be appreciated! 任何帮助,将不胜感激!

Use zip() like this: 像这样使用zip()

amp = ['a1', 'a2', 'a3', 'a4']
mass = ['m1', 'm2', 'm3', 'm4']
period = ['p1', 'p2', 'p3', 'p4']
ecc = ['e1', 'e2', 'e3', 'e4']
planet = [1, 2, 3, 4]


titles = ['planet', 'amp', 'mass', 'period', 'ecc']

print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*titles)
for item in zip(planet, amp, mass, period, ecc):
    print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*item)

Instead of using planet = [1, 2, 3, 4] , you can use enumerate() like below: 代替使用planet = [1, 2, 3, 4] ,可以使用如下的enumerate()

print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(*titles)
for i, item in enumerate(zip(amp, mass, period, ecc)):
    print '{:<6}|{:<6}|{:<6}|{:<6}|{:<6}'.format(i+1, *item)

Output: 输出:

>>> python print_table.py
planet|amp   |mass  |period|ecc
1     |a1    |m1    |p1    |e1
2     |a2    |m2    |p2    |e2
3     |a3    |m3    |p3    |e3
4     |a4    |m4    |p4    |e4

tabulate.tabulate() takes a list of lists (or arrays) as its main argument, you've given it a list of floats (and a '1'). tabulate.tabulate()将列表(或数组)列表作为其主要参数,您已为其指定了一个浮点列表(以及一个“ 1”)。

So you want to put in all your data at once. 因此,您想一次放入所有数据。

You'll need to create new lists where each is one horizontal row of the new table. 您需要创建新列表,其中每个列表都是新表的一个水平行。 You'll also need to add the row number at the front. 您还需要在前面添加行号。

First build new lists/arrays where: 首先构建新的列表/数组,其中:

list1=[1,a1,m1,p1,e1]
list2=[2,a2,m2,p2,e2]
...

And then try: 然后尝试:

print tabulate([list1, list2, list3, list4], headers=[...])

You can combine the usage of zip() with tabulate to create a nicer looking table: 您可以将zip()的用法与制表结合起来,以创建外观更好的表:

from tabulate import tabulate
headers = ['planet', 'amp', 'mass', 'period', 'ecc']    

amp = [1.1, 1.2, 1.3, 1.4]
mass = [2.1, 2.2, 2.3, 2.4]
period = [3.1, 3.2, 3.3, 3.4]
ecc = [4.1, 4.2, 4.3, 4.4]
planet = range(1, len(amp)+1)

table = zip(planet, amp, mass, period, ecc)
print(tabulate(table, headers=headers, floatfmt=".4f"))

Output: 输出:

  planet     amp    mass    period     ecc
--------  ------  ------  --------  ------
       1  1.1000  2.1000    3.1000  4.1000
       2  1.2000  2.2000    3.2000  4.2000
       3  1.3000  2.3000    3.3000  4.3000
       4  1.4000  2.4000    3.4000  4.4000

You can use \\t . 您可以使用\\t

For example: 例如:

print 'a\t','b\t','\n','c\t','d\t'

output: 输出:

a   b   
c   d   

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

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