问题
Basically, I want to make a grid with school subjects and all the test results I got from it, and I want to display about 10 results for every subject.
Like this:
...
--------------------------------------------------------------------
English| 7.4 | 6.4 | 9.5 | 4.5 | 8.9 | 3.9 | 8.0 | 6.5 | 9.9 | 4.9 |
--------------------------------------------------------------------
Dutch | Results
...
And I made basically two FOR loops, one reading every school subject out of a list and one that reads every result out of a list. However, I want them to complete without going "stuck" in the next loop. How do I do this? Do I thread the two loops and make a time delay so the values are readable every x seconds? (Probably not this, this is very slow)
Code:
...
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
EDIT:
With the solution (somewhat) down below, it will print all the test results, but It will print it wrong. How do I manage to print all the test results in one individual column/row? I would like all these number to be in the NTL (Dutch) row.
NTL | 7.2
ETL | 8.4
WIB | 6.7
WID | 5.3
ICT | 4.8
NAS | 9.4
SCK | 10.0
回答1:
If I understand correctly, your matrixcontent.dat
contains all the scores for each subject, one set per line, and the order of the lines correspond to the order of the subjects in your store
list variable.
In which case, you only need one loop, either over the lines in matrixcontent.dat
or over the store
variable.
Something like this ought to work...
with open("matrixcontent.dat", "r") as matrixcontent_open:
for item in store:
line = next(matrixcontent_open)
print(item + "|" + line + "\n" + ("-------------" * 7))
来源:https://stackoverflow.com/questions/17280529/python-double-for-loops-without-threading