Python double FOR loops without threading

2024/10/15 14:13:17

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 subjectswith open("matrixcontent.dat", "r") as matrixcontent_open:lines = matrixcontent_open.readlines() #Lines are test resultsfor 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

Answer

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://en.xdnf.cn/q/117822.html

Related Q&A

Challenging way of counting entries of a file dynamically

I am facing a strange question, which despite of trying many times, i am not able to find the logic and proper code to the problem. I have a file in the format below: aa:bb:cc dd:ee:ff 100 ---------…

NSException on import of matplotlib, kivy in OSX

Im working on some kivy code thats working fine on windows 10, but crashes on osx sierra, Ive isolated that the crash happens when I import kivy.core.window along side matplotlib: import matplotlib mat…

strange behavior with lamba: getattr(obj, x) inside a list [duplicate]

This question already has answers here:Creating functions (or lambdas) in a loop (or comprehension)(9 answers)Closed 11 years ago.In the following example:class A(object):passprop1 = 1prop2 = 2prop3 = …

Detect or Generate Regular Expression from String

I was wondering if there were any Python packages out there that detects a regular expression from a string. Conceptually this is easy enough to do but I wanted to see if there was anyone else who has …

Date regex python

I am trying to match dates in a string where the date is formatted as (month dd, yyyy). I am confused by what I see when I use my regex pattern below. It only matches strings that begin with a date. Wh…

Not able to install the python-module numpy

After hours of trying Im still not able to install numpy. I READ LOTS OF HINTS, ANSWERS USW. BUT IT DOESNT HELP. Furthermore I have windows 7, 32 bit, Python 27. What I did:download numpy-1.10.2.zi…

Windows Error: 32 when trying to rename file in python

Im trying to rename some PDF files using pyPdf and my code it seems to work fine until it reaches the rename sentence. The While/if block of code looks for the page number where string "This stri…

I dont quite understand the while loop in python

def AddSingleCard(self):symbols = [heart, diamond, club, spade]#newCardSign = newCardNumber, newCardSign = raw_input().split()try:newCardNumber = int(float(newCardNumber))except:newCardNumber, newCardS…

Adding a FileField to a custom SignupForm with django-allauth

I have the following custom SignupForm (simplified, works perfectly without my_file):class SignupForm(forms.Form):home_phone = forms.CharField(validators=[phone_regex], max_length=15)my_file = forms.Fi…

Checkbox to determine if an action is completed or not

I have a list of dictionaries of clients in a format like this:dict_list = [{Name of Business : Amazon, Contact Name : Jeff Bezos, Email : [email protected]}, {Name of Business : Microsoft, Contact Nam…