有什么更简洁、更Python式的方法来进行以下枚举?

发布于 2024-08-14 20:18:50 字数 162 浏览 6 评论 0原文

for row, instrument in enumerate(instruments):
    for col, value in enumerate(instrument):
         self.table.SetValue(row, col, value)
for row, instrument in enumerate(instruments):
    for col, value in enumerate(instrument):
         self.table.SetValue(row, col, value)

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

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

发布评论

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

评论(4

烟雨凡馨 2024-08-21 20:18:50

您所说的 row 不是行,而是行索引。 instrument 是一行。除此之外:

如果您拥有的唯一工具是 SetValue(row_index, column_index, value) 方法,并且该方法的作用不仅仅是帮助复制仪器的结构 费力,并且 instruments 集合正如您的代码所描述的那样,那么没有更好的方法了。 要好得多

#WARNING: BAD PRACTICE! DON'T USE THIS CODE!
for row_index in xrange(len(instruments)):
    instrument = instruments[row_index]
    for column_index in xrange(len(instrument)):
        self.table.SetValue(row_index, column_index, instrument[column_index])

它已经比任何其变态

。否则您可能对导入副本感兴趣; self.table = copy.copy(instruments) 甚至(如 SilentGhost 建议的那样)self.table = Instruments

What you are calling row is not a row, it is a row index. instrument is a row. Apart from that:

If the only tool that you have is a SetValue(row_index, column_index, value) method and that method does more than help replicate the structure of instruments laboriously, and the instruments collection is as your code describes, then there is no better way. It is already much better than

#WARNING: BAD PRACTICE! DON'T USE THIS CODE!
for row_index in xrange(len(instruments)):
    instrument = instruments[row_index]
    for column_index in xrange(len(instrument)):
        self.table.SetValue(row_index, column_index, instrument[column_index])

or any perversion thereof.

Otherwise you may be interested in import copy; self.table = copy.copy(instruments) or even (as SilentGhost has suggested) self.table = instruments

风为裳 2024-08-21 20:18:50

您在评论中提到该循环是异步函数的一部分(就扭曲框架而言)。在这种情况下,您不想长时间阻塞:

from twisted.internet import task

for i, row in enumerate(instruments):
    task.coiterate(self.table.SetValue(i, j, v) for j, v in enumerate(row))

因此所有行都是并行分配的。

注意:

  • 请注意 irow 的后期绑定。在这种情况下,请使用 (lambda i=i, row=row: ...)()
  • task.coiterate() 使用全局对象,因此可能会同时更新多个表(这可能不是您想要的)。

这是 @SilentGhost 的答案< /a>(已删除):

self.table = 工具

因为这就是你看起来的样子
正在做。

我指的是 @[Ben Hughes] 的评论:

我需要显式调用 SetValue (其
在 PyGridTableBase 上)对于每个值 -
因为这段代码是通过扭曲调用的
延迟方法——我脑子不多
擅长整齐地循环/枚举
方式...... – 本休斯

You've mentioned in the comments that this loop is a part of asynchronous function (in terms of twisted framework). In this case you don't want to block for a long time:

from twisted.internet import task

for i, row in enumerate(instruments):
    task.coiterate(self.table.SetValue(i, j, v) for j, v in enumerate(row))

Thus all rows are assigned in parallel.

NOTE:

  • Watch out for late binding for i and row. Use (lambda i=i, row=row: ...)() in that case.
  • task.coiterate() uses global object therefore there could be multiple table updates simultaneously (it might not be what you want).

Here's @SilentGhost' answer (deleted):

self.table = instruments

Because that's what you seem to be
doing.

And the comment by @[Ben Hughes] I'm referring to:

I need to explicity call SetValue (its
on a PyGridTableBase) for each value -
as this code is invoked via a twisted
deferred method - my brain is not much
good at looping/enumeration in a neat
way..... – Ben Hughes

乱了心跳 2024-08-21 20:18:50

根本问题不在于这个循环。

基本问题如下:

1)这个instruments结构从哪里来,为什么需要重新组织它?

2) 您调用 SetValue 的这个 self.table 结构是什么?

3) 你打算用这个 self.table 结构做什么?

在您回答这些问题之前,您的示例 Python 代码没有可以对其进行评估的上下文。这只是代码。

The fundamental question is not regarding this loop.

The fundamental questions are these:

1) Where does this instruments structure come from, and why do you need to reorganize it?

2) What is this self.table structure on which you're calling SetValue?

3) What are you going to do with this self.table structure?

Until you answer these questions, your sample Python code has no context in which it can be evaluated. It's just code.

灼疼热情 2024-08-21 20:18:50

一个选择是使用列表理解:

[self.table.setValue(row, col, value) 
    for row, instrument in enumerate(instruments) 
        for col, value in enumerate(instrument)]

不确定它是否更简洁或更Pythonic...但它是表达循环的另一种方式。

有人可能会说列表理解更清晰,因为“动作”部分(setValue)被放置在第一/顶部。而不是被埋在循环内。

编辑:

还有另一种方法,使用辅助函数和生成器表达式:

def loop(iterator):
    for item in iterator: pass

loop(self.table.setValue(row, col, value) 
    for row, instrument in enumerate(instruments) 
        for col, value in enumerate(instrument))

如果您确定循环体永远不会在 a 中计算为 True,则辅助函数可以是内置的 any()布尔上下文(any() 将在第一个 True 时停止迭代)。

One option is using a list comprehension:

[self.table.setValue(row, col, value) 
    for row, instrument in enumerate(instruments) 
        for col, value in enumerate(instrument)]

Not sure if it is any neater or more pythonic... But it is another way of expressing the loop.

One could argue that the list comprehension is clearer, since the "action" part (setValue) is placed first/at the top. Instead of being buried inside the loop.

EDIT:

Yet another way, using a helper function and a generator expression:

def loop(iterator):
    for item in iterator: pass

loop(self.table.setValue(row, col, value) 
    for row, instrument in enumerate(instruments) 
        for col, value in enumerate(instrument))

The helper function can be the built-in any() if you know for certain that the loop body will never evaluate to True in a boolean context (any() will stop the iteration on the first True).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文