打印带标题的表格时替代 locals()

发布于 2024-09-29 21:46:52 字数 1172 浏览 1 评论 0原文

[Python 3.1]

编辑:原始代码中的错误。

我需要打印一张表格。第一行应该是标题,由制表符分隔的列名称组成。以下行应包含数据(也以制表符分隔)。

为了澄清起见,假设我有“速度”、“功率”、“重量”列。我最初在我之前提出的相关问题的帮助下编写了以下代码:

column_names = ['speed', 'power', 'weight']

def f(row_number):
  # some calculations here to populate variables speed, power, weight
  # e.g., power = retrieve_avg_power(row_number) * 2.5
  # e.g., speed = math.sqrt(power) / 2
  # etc.
  locals_ = locals()
  return {x : locals_[x] for x in column_names}

def print_table(rows):
  print(*column_names, sep = '\t')
  for row_number in range(rows):
    row = f(row_number)
    print(*[row[x] for x in component_names], sep = '\t')

但后来我了解到我应该避免使用locals() 如果可能的话

现在我被困住了。我不想多次键入所有列名称的列表。我不想依赖这样一个事实:我在 f() 中创建的每个字典都可能以相同的顺序迭代其键。而且我不想使用 locals()

请注意,函数 print_table()f() 还执行许多其他操作;所以我必须把它们分开。

代码应该怎么写呢?

[Python 3.1]

Edit: mistake in the original code.

I need to print a table. The first row should be a header, which consists of column names separated by tabs. The following rows should contain the data (also tab-separated).

To clarify, let's say I have columns "speed", "power", "weight". I originally wrote the following code, with the help from a related question I asked earlier:

column_names = ['speed', 'power', 'weight']

def f(row_number):
  # some calculations here to populate variables speed, power, weight
  # e.g., power = retrieve_avg_power(row_number) * 2.5
  # e.g., speed = math.sqrt(power) / 2
  # etc.
  locals_ = locals()
  return {x : locals_[x] for x in column_names}

def print_table(rows):
  print(*column_names, sep = '\t')
  for row_number in range(rows):
    row = f(row_number)
    print(*[row[x] for x in component_names], sep = '\t')

But then I learned that I should avoid using locals() if possible.

Now I'm stuck. I don't want to type the list of all the column names more than once. I don't want to rely on the fact that every dictionary I create inside f() is likely to iterate through its keys in the same order. And I don't want to use locals().

Note that the functions print_table() and f() do a lot of other stuff; so I have to keep them separate.

How should I write the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反目相谮 2024-10-06 21:46:52
class Columns:
    pass

def f(row_number):
    c = Columns()
    c.power = retrieve_avg_power(row_number) * 2.5
    c.speed = math.sqrt(power) / 2
    return c.__dict__

这还允许您指定哪些变量作为列,而不是在函数中临时存在。

class Columns:
    pass

def f(row_number):
    c = Columns()
    c.power = retrieve_avg_power(row_number) * 2.5
    c.speed = math.sqrt(power) / 2
    return c.__dict__

This also lets you specify which of the variables are meant as columns, instead of rather being temporary in the function.

滴情不沾 2024-10-06 21:46:52

您可以使用 OrderedDict 来修复字典的顺序。但据我看来,这甚至没有必要。您始终从 column_names 列表中获取键(除了最后一行,我认为这是一个拼写错误),因此值的顺序将始终相同。

You could use an OrderedDict to fix the order of the dictionaries. But as I see it that isn't even necessary. You are always taking the keys from the column_names list (except in the last line, I assume that is a typo), so the order of the values will always be the same.

心头的小情儿 2024-10-06 21:46:52

locals() 的替代方法是使用 inspect 模块

import inspect

def f(row_number):
    # some calculations here to populate variables speed, power, weight
    # e.g., power = retrieve_avg_power(row_number) * 2.5
    # e.g., speed = math.sqrt(power) / 2
    # etc.
    locals_ = inspect.currentframe().f_locals
    return {x : locals_[x] for x in column_names }

an alternative to locals() will be to use the inspect module

import inspect

def f(row_number):
    # some calculations here to populate variables speed, power, weight
    # e.g., power = retrieve_avg_power(row_number) * 2.5
    # e.g., speed = math.sqrt(power) / 2
    # etc.
    locals_ = inspect.currentframe().f_locals
    return {x : locals_[x] for x in column_names }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文