有人能解释一下如何使用repr函数格式化输出吗?

0 投票
1 回答
1091 浏览
提问于 2025-04-18 10:39

我想要以下的代码:

Tier0 = ['Tier', 'Weights', 'Price', 'Number of Businesses', 'Revenue']
Tier1 = ['Tier 1', 180,]
Tier2 = ['Tier 2', 300,]
Tier3 = ['Tier 3', 450,]
Tier4 = ['Tier 4', 600,]
Tier5 = ['Tier 5', 750,]

data = []
data.append(Tier0)
data.append(Tier1)
data.append(Tier2)
data.append(Tier3)
data.append(Tier4)
data.append(Tier5)    
data

for Tier1 in data[1:]: 
    Tier1.insert(1, float(input('Enter the weighted value of Tiers 1-5 as a decimal: ')))
    Tier1.insert(3, Tier1[1] * MissouriBusiness)#calculates the number of businesses
    Tier1.insert(4, Tier1[2] * Tier1[1] * MissouriBusiness)#calculates the revenue

输出结果现在是:

[['Tier', 'Weights', 'Price', 'Number of Businesses', 'Revenue'], ['Tier 1', 0.2, 180, 40000.0, 7200000.0], ['Tier 2', 0.1, 300, 20000.0, 6000000.0], ['Tier 3', 0.3, 450, 60000.0, 27000000.0], ['Tier 4', 0.15, 600, 30000.0, 18000000.0], ['Tier 5', 0.2, 750, 40000.0, 30000000.0]]

我希望能改成更好看的样子,比如:

[['Tier', 'Weights', 'Price', 'Number of Businesses', 'Revenue'], 
['Tier 1', 0.2, 180, 14000.0, 2520000.0], 
['Tier2', 0.2, 300, 14000.0, 4200000.0], 
['Tier3', 0.2, 450, 14000.0, 6300000.0], 
['Tier4', 0.3, 600, 21000.0, 12600000.0], 
['Tier5', 0.1, 750, 7000.0, 5250000.0]] 

Python的文档里有一个很好的例子,但我就是搞不懂它是怎么工作的。

>>> for x in range(1, 11):
...     print repr(x).rjust(2), repr(x*x).rjust(3),
...     # Note trailing comma on previous line
...     print repr(x*x*x).rjust(4)
...
 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125

这段代码来自Python的文档:https://docs.python.org/2/tutorial/inputoutput.html

如果能简单明了地解释一下这个repr函数是怎么工作的,以及我该如何使用它,我会非常感激,因为每次看文档或者其他类似的问题时,我都觉得一头雾水。

谢谢大家!

1 个回答

0

repr 函数的作用是返回“可打印的对象表示”,这通常是为了配合 eval 函数使用,能够根据字符串表示重新构建对象。不过,repr 本身并不进行任何格式化,它只是调用对象的 __repr__ 方法,这个方法可能是 Python 默认的实现,也可能是你自己为对象定义的实现。

在你的例子中,当你对一个列表调用 repr 时,它只是返回带引号的列表输出,因为这就是 repr 对列表对象的实现方式:

>>> data = [['Tier', 'Weights', 'Price', 'Number of Businesses', 'Revenue'], ['Tier 1', 0.2, 180, 40000.0, 7200000.0], ['Tier 2', 0.1, 300, 20000.0, 6000000.0], ['Tier 3', 0.3, 450, 60000.0, 27000000.0], ['Tier 4', 0.15, 600, 30000.0, 18000000.0], ['Tier 5', 0.2, 750, 40000.0, 30000000.0]]
>>> repr(data)
"[['Tier', 'Weights', 'Price', 'Number of Businesses', 'Revenue'], ['Tier 1', 0.2, 180, 40000.0, 7200000.0], ['Tier 2', 0.1, 300, 20000.0, 6000000.0], ['Tier 3', 0.3, 450, 60000.0, 27000000.0], ['Tier 4', 0.15, 600, 30000.0, 18000000.0], ['Tier 5', 0.2, 750, 40000.0, 30000000.0]]"

你提供的 repr 示例其实只是手动格式化而已。在这种情况下,repr 只是用来获取整数 x 的字符串表示,以便能够对其调用 string.rjust 格式化函数:

>>> repr(1)
'1'
>>> repr(1).rjust(3)
'  1'

至于你的情况,你可以使用 pprint 模块,它是专门用来美观打印 Python 数据结构的。pprint.pprint 函数实际上几乎完全符合你的需求:

>>> data = [['Tier', 'Weights', 'Price', 'Number of Businesses', 'Revenue'], ['Tier 1', 0.2, 180, 40000.0, 7200000.0], ['Tier 2', 0.1, 300, 20000.0, 6000000.0], ['Tier 3', 0.3, 450, 60000.0, 27000000.0], ['Tier 4', 0.15, 600, 30000.0, 18000000.0], ['Tier 5', 0.2, 750, 40000.0, 30000000.0]]
>>> import pprint
>>> pprint.pprint(data)
[['Tier', 'Weights', 'Price', 'Number of Businesses', 'Revenue'],
 ['Tier 1', 0.2, 180, 40000.0, 7200000.0],
 ['Tier 2', 0.1, 300, 20000.0, 6000000.0],
 ['Tier 3', 0.3, 450, 60000.0, 27000000.0],
 ['Tier 4', 0.15, 600, 30000.0, 18000000.0],
 ['Tier 5', 0.2, 750, 40000.0, 30000000.0]]

不过如果你需要一些非常特定的格式,可能就需要自己实现打印功能了。

撰写回答