【问题标题】:Python printing lists side by sidePython并排打印列表
【发布时间】:2016-07-15 21:41:03
【问题描述】:

每个人。我是 Python 新手,急需一些帮助。我正在尝试计算几个列表中的唯一值,然后将输出并排打印为列。我可以使用集合很好地计算它们。但是,我不知道如何并排打印它们。有没有什么 Pythonic 方法可以将它们串联或并排显示为列?

我尝试了以下但无济于事。非常感谢任何帮助。

print(str(parsed_list(a)) + str(parsed_list(b)) + str(parsed_list(b)))
NoneNoneNone

我的可测试代码示例(Python3):

import collections, operator

a = ['Black Cat', 'Black Dog', 'Black Mouse']
b = ['Bird', 'Bird', 'Parrot']
c = ['Eagle', 'Eagle', 'Eagle', 'Hawk']


def parsed_list(list):
    y = collections.Counter(list)
    for k, v in sorted(y.items(), key=operator.itemgetter(1), reverse=True):
        z = (str(k).ljust(12, ' ') + (str(v)))
        print(z)

print('Column1          Column2             Column3')
print('-' * 45)
parsed_list(a)
parsed_list(b)
parsed_list(c)

当前:

Column1          Column2             Column3
---------------------------------------------
Black Cat   1
Black Dog   1
Black Mouse 1
Bird        2
Parrot      1
Eagle       3
Hawk        1

期望的输出:

Column1          Column2      Column3
----------------------------------------
Black Cat   1    Bird     2   Eagle  3
Black Dog   1    Parrot   1   Hawk   1
Black Mouse 1

【问题讨论】:

  • 您是否考虑过使用 Pandas 处理类似的表格数据?
  • @cricket_007 不幸的是,这个脚本是在我无法(不允许)安装 Pandas 的系统上执行的。没有本地方法可以做到这一点吗?似乎是经常需要的东西。

标签: python dictionary multiple-columns


【解决方案1】:

依赖tabulate module:

import collections
from itertools import zip_longest
import operator

from tabulate import tabulate

def parsed_list(lst):
    width = max(len(item) for item in lst)
    return ['{key} {value}'.format(key=key.ljust(width), value=value)
            for key, value in sorted(
                collections.Counter(lst).items(),
                key=operator.itemgetter(1), reverse=True)]

a = parsed_list(['Black Cat', 'Black Dog', 'Black Mouse'])
b = parsed_list(['Bird', 'Bird', 'Parrot'])
c = parsed_list(['Eagle', 'Eagle', 'Eagle', 'Hawk'])

print(tabulate(zip_longest(a, b, c), headers=["Column 1", "Column 2", "Column 3"]))

# Output:
# Column 1       Column 2       Column 3
# -------------  -------------  -------------
# Black Mouse 1  Bird        2  Eagle       3
# Black Dog   1  Parrot      1  Hawk        1
# Black Cat   1

【讨论】:

    【解决方案2】:
    from collections import Counter
    from itertools import zip_longest
    a = ['Black Cat', 'Black Dog', 'Black Mouse']
    b = ['Bird', 'Bird', 'Parrot']
    c = ['Eagle', 'Eagle', 'Eagle', 'Hawk']
    
    def parse(lst):
        c = Counter(lst)
        r = []
        for s, cnt in c.most_common():
            r += ['%12s%3i' % (s, cnt)]
        return r 
    
    for cols in zip_longest(parse(a), parse(b), parse(c), fillvalue=15*' '):
        print(' '.join(cols))
    

    这会产生:

       Black Cat  1         Bird  2        Eagle  3
     Black Mouse  1       Parrot  1         Hawk  1
       Black Dog  1                                
    

    【讨论】:

      【解决方案3】:

      我确定您可以手动构建自己的库,但 Python 似乎在字符串类型中内置了一种格式方法,可以很好地满足您的目的。

      这个link之前的帖子可以帮助你!试一试!

      【讨论】:

        猜你喜欢
        • 2020-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-04
        • 2011-04-05
        • 1970-01-01
        • 1970-01-01
        • 2015-01-04
        相关资源
        最近更新 更多