【问题标题】:Python double FOR loops without threading没有线程的Python双FOR循环
【发布时间】:2013-06-24 16:36:18
【问题描述】:

基本上,我想用学校科目和我从中获得的所有测试结果制作一个网格,我想为每个科目显示大约 10 个结果。

像这样:

...
--------------------------------------------------------------------
English| 7.4 | 6.4 | 9.5 | 4.5 | 8.9 | 3.9 | 8.0 | 6.5 | 9.9 | 4.9 |
--------------------------------------------------------------------
Dutch  | Results
...

我基本上做了两个 FOR 循环,一个从列表中读取每个学校科目,另一个从列表中读取每个结果。 但是,我希望它们完成而不会在下一个循环中“卡住”。 我该怎么做呢?我是否将两个循环穿线并进行时间延迟,以便每 x 秒读取一次值? (可能不是这个,这个很慢)

代码:

...
for item in store: #Loop that reads the subjects
        with open("matrixcontent.dat", "r") as matrixcontent_open:
            lines = matrixcontent_open.readlines() #Lines are test results
        for line in lines:
            print(item + "|" + line + "\n" + ("-------------" * 7))
            #I want this last line to print the subject and than all the results

编辑:

使用下面的解决方案(有点),它将打印所有测试结果,但它会打印错误。如何将所有测试结果打印在一个单独的列/行中?我希望所有这些数字都在 NTL(荷兰语)行中。

NTL | 7.2


ETL | 8.4


WIB | 6.7


WID | 5.3


信息通信技术 | 4.8


NAS | 9.4


SCK | 10.0

【问题讨论】:

  • “卡住”是什么意思?
  • @kindall 它将在存储循环中开始,然后在行循环中继续并留在那里直到完成。我希望它一个接一个地完成,但不是在第一个完成之后。
  • 所以matrixcontent.dat 有您所有商品的数据吗?结尾有新内容吗?
  • @cmd matrixcontent.dat 包含了所有的测试结果,没有添加新的内容。

标签: python multithreading for-loop grid multitasking


【解决方案1】:

如果我理解正确,您的matrixcontent.dat 包含每个科目的所有分数,每行一组,并且行的顺序与您的store 列表变量中的科目顺序相对应。

在这种情况下,您只需要一个循环,无论是在matrixcontent.dat 中的行上还是在store 变量上。

这样的事情应该可以工作......

with open("matrixcontent.dat", "r") as matrixcontent_open:
    for item in store:
        line = next(matrixcontent_open)
        print(item + "|" + line + "\n" + ("-------------" * 7))

【讨论】:

  • 这听起来合乎逻辑,但我收到了 line = next(matrixcontent_open) StopIteration 错误。
  • @IPDGino 看起来我猜错了。这将有助于在 OP 中指定 matrixcontent.dat 文件的格式。
猜你喜欢
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多