迭代内的多个条件,Python

发布于 2024-10-07 20:35:38 字数 754 浏览 0 评论 0原文

我正在为一些软件编写一个附加组件,使用它的 API。我需要做的是提取必要的数据。 我使用“FOR”来思考 API 类。每个对象都有属性:索引(从 0 开始)、类型(Lin、Ptp 和其他一些)和值。浏览对象列表,我对两种类型的对象感兴趣 - 那些类型为“Lin”或“Ptp”的对象;所以需要满足几个条件:

对于Lin类型:

  • 如果Lin之前有一些Ptp(尽管它们之间可能存在其他类型的其他对象),Lin得到Ptp的值[Ptp....Lin]。
  • 如果 Lin 之前还有其他 Lin(不过,它们之间可能存在其他类型的其他对象),Lin 会获得前一个最接近的 Lin 的值 [Lin....Lin]。
  • 如果 Lin 之前既没有 Lin 也没有 Ptp(尽管它们之间可能存在其他类型的其他对象),Lin 的值为“0”[...Lin]。

至于Ptp类型,它总是有自己的价值,

因为我是Python的初学者,我现在的想法很复杂,我无法想出合适的算法。

我想应该是这样的:

for object in obects:
   If object.type == Ptp:
     ...object gets its own value
   elif object.type == Lin:
     ...

这里,应该有其他3个条件根据 [...Lin][Lin...Lin] 或 <代码>[Ptp...林]

I am writing an add-on for some softwate, using its API. What i need to do is to extract necessary data.
I use 'FOR' to go thought API classes. Each object has properties: index (from 0), type (Lin, Ptp, and some others), and value. Going through list of objects, I am interested in two types of objects - those that have type 'Lin' or 'Ptp'; so a few conditions should be met:

As to Lin type:

  • if there is some Ptp before Lin (there may be other objects of other types between them, though), Lin gets Ptp's value [Ptp....Lin].
  • if there is some other Lin before Lin (there may be other objects of other types between them, though), Lin gets that previous closest Lin's value [Lin....Lin].
  • if there is neither Lin nor Ptp before Lin (there may be other objects of other types between them, though), Lin gets value "0" [...Lin].

As to Ptp type, it always gets its own value

As i am a beginner in Python, my thoughs are mixed now and i cannot come up with appropriate algorithm.

I was thinking it should be somthing like this:

for object in obects:
   If object.type == Ptp:
     ...object gets its own value
   elif object.type == Lin:
     ...

Here, there should be other 3 conditions according to [...Lin] or [Lin...Lin] or [Ptp...Lin]

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

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

发布评论

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

评论(3

成熟稳重的好男人 2024-10-14 20:35:38

由于我是Python的初学者,我现在的想法很复杂,我无法想出合适的算法。

如果您试图想出一个合适的算法,请后退一步忘记 Python(C++、Fortran、Logo、Awk 等...)并思考您要解决的问题。尝试在纸上写一些伪代码。

从您的伪代码来看,Python 应该变得更加明显,并且任何技术都可以在 StackOverflow 上作为更具体的问题提出(例如)或询问您的同事。

As i am a beginner in Python, my thoughs are mixed now and i cannot come up with appropriate algorithm.

If you are trying to come up with an appropriate algorithm, take a step back. Forget Python (C++, Fortran, Logo, Awk, etc...) and think about the problem you are trying to solve. Try writing some pseudocode on paper.

From your pseudocode, the Python should become more apparent and any technical difficulties could be posed as more specific questions on StackOverflow (for instance) or asked of your colleagues.

一瞬间的火花 2024-10-14 20:35:38

我建议您迭代您的对象,并记住 LinPtp 类型的最后一次出现(无论它们是什么......:)):

lastOccurrence = None
for obj in objects:
    if obj.type not in ('PtP', 'Lin'):
        continue

    if obj.type == 'Lin':
        if lastOccurrence is not None:
            obj.value = lastOccurrence.value
        else:
            obj.value = "0"

    lastOccurrence = obj                

或类似的东西那 ...

I'd suggest you iterate over your objects, and remember the last occurrence of a Lin or Ptp type (whatever those may be ... :)):

lastOccurrence = None
for obj in objects:
    if obj.type not in ('PtP', 'Lin'):
        continue

    if obj.type == 'Lin':
        if lastOccurrence is not None:
            obj.value = lastOccurrence.value
        else:
            obj.value = "0"

    lastOccurrence = obj                

or something like that ...

别低头,皇冠会掉 2024-10-14 20:35:38

我会使用称为 有限状态机 或 FSM 的东西来遍历 API 对象。当您遇到不同的类型和关联的属性时,您可以存储有关 FSM“状态”中所见内容的信息,这也确定了下一个“事件”(您正在迭代的项目)发生或遇到时会发生什么。收集到的信息可以根据需要输出(即,当达到某种状态时)。FSM

是一个相当简单的学习和编程概念(几乎用任何语言),对于此类问题非常有用。

I'd use something called a finite-state machine or FSM to go though the API objects. As you encounter the different types and associated properties you can store information about what was seen in the FSM's "state" which also determines what happens when the next "event" (items you are iterating over) is occurs or is encountered. Information gathered can be output as required (i.e. when a certain state is reached.

FSM's are a fairly simple concept to lean and program (in almost any language) and very useful for this sort of problem.

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