python 漂亮地以表格格式打印列表 [英] python Pretty printing a list in a tabular format

查看:86
本文介绍了python 漂亮地以表格格式打印列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是这里的一个帖子 漂亮地打印一个列表表格格式

mylist = [ ( (12, 47, 4, 574862, 58, 7856), 'AGGREGATE_VALUE1'),( (2, 75, 757, 8233, 838, 47775272785), 'AGGREG2'),( (4144, 78, 78965, 778, 78578, 2), 'AGGREGATE_VALUE3')]header = ('Price1','Price2','reference','XYD','code','resp','AGGREG values')longg = dict(zip((0,1,2,3,4,5,6),(len(str(x)) for x in header)))对于 tu,x 在 mylist 中:longg.update(( i, max(longg[i],len(str(el))) ) for i,el in enumerate(tu))longg[6] = max(longg[6],len(str(x)))fofo = ' |'.join('%%-%ss' % longg[i] for i in xrange(0,7))打印 '\n'.join((fofo % 标题,'-|-'.join( longg[i]*'-' for i in xrange(7)),'\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist)))

我想替换 '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g在 mylist))) 中带有一些动态的东西.例如 range1 = ', '.join(map(chr, range(97, 97+7))) 这给了我 a,b,c,d,e,f,g

所以该行看起来 '\n'.join(fofo % (range1) for (a,b,c,d,e,f),g in mylist)))>

但是给了我:

<块引用>

TypeError: 没有足够的格式字符串参数

解决方案

错误是 range1 是一个字符串,而您希望有变量.您也许可以使用类似 eval?

之类的东西

所以你可以这样做:

 '\n'.join(fofo % eval(range1) for (a,b,c,d,e,f),g in mylist)

或者避免引用多个变量:

 '\n'.join(fofo % (tuple(x[0])+(x[1],)) for x in mylist)

mylist 的每个元素都由一个元组组成,该元组包含一个元组和一个字符串,如 ((a,b..), "string").因此,如果您考虑 x 包含此类数据, tuple(x[0]) 指定第一个元组,而 (x[1],) 使用单个元素创建一个元组,即字符串.最后 tuple(x[0])+(x[1],) 将只创建一个包含所有元素的元组,例如:

 x = ((0, 1, 2), "a")tuple(x[0])+(x[1],) #相当于(0, 1, 2, "a")

这将适用于 ((0, 1, 2, 3, 4), "a") 或内部元组中的任意数量的元素.

I am referring to a post here Pretty printing a list in a tabular format

mylist = [ ( (12, 47, 4, 574862, 58, 7856), 'AGGREGATE_VALUE1'),
           ( (2, 75, 757, 8233, 838, 47775272785), 'AGGREG2'),
           ( (4144, 78, 78965, 778, 78578, 2), 'AGGREGATE_VALUE3')]

header = ('Price1','Price2','reference','XYD','code','resp','AGGREG values')

longg = dict(zip((0,1,2,3,4,5,6),(len(str(x)) for x in header)))

for tu,x in mylist:
    longg.update(( i, max(longg[i],len(str(el))) ) for i,el in enumerate(tu))
    longg[6] = max(longg[6],len(str(x)))
fofo = ' | '.join('%%-%ss' % longg[i] for i in xrange(0,7))

print '\n'.join((fofo % header,
                 '-|-'.join( longg[i]*'-' for i in xrange(7)),
                 '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist)))

I want to replace '\n'.join(fofo % (a,b,c,d,e,f,g) for (a,b,c,d,e,f),g in mylist))) with something dynamic. For example range1 = ', '.join(map(chr, range(97, 97+7))) which gives me a,b,c,d,e,f,g

so the line will look '\n'.join(fofo % (range1) for (a,b,c,d,e,f),g in mylist)))

but gives me:

TypeError: not enough arguments for format string

解决方案

The error is that range1 is a string while you expect to have variables. You perhaps may use something like eval?

So you can do:

    '\n'.join(fofo % eval(range1) for (a,b,c,d,e,f),g in mylist)

Or to avoid refering to several vars:

    '\n'.join(fofo % (tuple(x[0])+(x[1],)) for x in mylist)

Each element of mylist is made by a tuple containing a tuple and a string like ((a,b..), "string"). So if you consider x containing such data, tuple(x[0]) designate the first tuple while (x[1],) make a tuple with single element that is the string. Finally tuple(x[0])+(x[1],) will create only one tuple with all elements like:

    x = ((0, 1, 2), "a")
    tuple(x[0])+(x[1],)  #is equivalent to (0, 1, 2, "a")

This will work with ((0, 1, 2, 3, 4), "a"), or any number of element in the inside tuple.

这篇关于python 漂亮地以表格格式打印列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆