Python表格对齐/填充?

时间:2017-03-05 21:30:15

标签: python alignment

我有一个当前采用列表的函数,并创建一个表,计算每个项目在列表中出现的次数。当我使用单个字符项运行它时,频率列以一个漂亮的偶数行返回。但是当项目不止一个字符时,频率全部不对齐。我怎样才能在Python中解决这个问题?

以下是两张示例的结果图片(因为我无法弄清楚如何制作表格......):

enter image description here

freqTable([1, 2, 3, 3, 1, 4, 5]) 

freqTable(['CIS', 'CIS', 'CIS', 'J', 'Psy', 'Psy'])

如何修复路线?

编辑:它由3个功能组成

def drawTable(freqD):

    iteml = list(freqD.keys())
    iteml.sort()

    title = 'ITEM' + (' '*2) + 'FREQUENCY'
    print(title)

    for item in iteml:
        print(item, ' '*3, freqD[item])

    return None

def genFreqTable(alist):

    freqD = {}

    for item in alist:
        if item in freqD:
            freqD[item] += 1
        else:
            freqD[item] = 1

    return freqD


def freqTable(alist):

    freqD = genFreqTable(alist)
    drawTable(freqD)

    return None

继承测试用例 freqTable([1,2,3,4]) freqTable(['CIS','CIS','CIS','J','Psy','Psy'

2 个答案:

答案 0 :(得分:0)

您必须使用格式化输出进行打印,例如:

print("%-4s\t%3d" % (item, freqD[item]))

答案 1 :(得分:0)

您的问题对于命名两个同名的 tulpes 有点令人困惑,名称相同 freqTable

freqTable([1, 2, 3, 3, 1, 4, 5])

freqTable(['CIS', 'CIS', 'CIS', 'J', 'Psy', 'Psy'])

通常最后一个声明会覆盖另一个。

关于

#tuples and lists are the same thing, but a tuple cannot be changed
# a list variable : [] or <varname> : mylist
mylist : list = [] 

# a tuple : () or <varname> : mytuple
mytuple : tuple = () 

表格中元素的正确对齐:


带有colorama的颜色:

from colorama import Fore, Style


coins = [
    ["NAME", "#", "Price", "Change (%)"],
    ["Bitcoin", 1, "$7,430.07", 1.22],
    ["Ethereum", 2, "$461.02", -3.43],
    ["XRP", 3, "$0.46150", -3.90],
    ["Bitcoin Cash", 4, "$804.32", -1.70],
    ["EOS", 5, "$8.11", -4.29],
    ["Stellar", 6, "$0.29635", 0.91],
]

dash = "-" * 60


def color(val):

    if val > 0:
        out = Fore.RED + str(val) + Style.RESET_ALL
    else:
        out = Fore.GREEN + str(val) + Style.RESET_ALL
    return out


for i in range(len(coins)):
    if i == 0:
        print(dash)
        print(
            "{:<15s}{:>6s}{:>20s}{:>15s}".format(
                coins[i][0], coins[i][1], coins[i][2], coins[i][3]
            )
        )
        print(dash)
    else:
        print(
            "{:<15s}{:>6d}{:>20s}{:>21s}".format(
                coins[i][0], coins[i][1], coins[i][2], color(coins[i][3])
            )
        )

没有颜色(删除导入colorama

def color(val):
return str(val)

输出:

------------------------------------------------------------
NAME                #               Price     Change (%)
------------------------------------------------------------
Bitcoin             1           $7,430.07        1.22
Ethereum            2             $461.02       -3.43
XRP                 3            $0.46150        -3.9
Bitcoin Cash        4             $804.32        -1.7
EOS                 5               $8.11       -4.29
Stellar             6            $0.29635        0.91

对比

------------------------------------------------------------
NAME, #, Price, Change (%)
------------------------------------------------------------
Bitcoin, 1, $7,430.07, 1.22
Ethereum, 2, $461.02, -3.43
XRP, 3, $0.46150, -3.9
Bitcoin Cash, 4, $804.32, -1.7
EOS, 5, $8.11, -4.29
Stellar, 6, $0.29635, 0.91

REF - 更多信息: