Answer the question
In order to leave comments, you need to log in
How to create sheets in Python from objects in a sheet based on a regular expression applied to a specific object data variable?
There is a Course class with section variable. The section variable is a regular string of the form <number><abbreviation, example: L, Lb, R>. For example, 2R. By reduction, you need to divide the sheet into subsheets R, L, etc. (number of abbreviations not specified).
Regular expression:
For example: before doing this, I form sublists based on another variable abbr using defaultdict:reg = re.compile('(?:\d+)([a-zA-Z]+)')
result = defaultdict(list)
for obj in inlist:
result[obj.abbr].append(obj)
inlist = list(result.values())
Answer the question
In order to leave comments, you need to log in
Very good question, I love these. What, why - you will understand the hell. You can immediately see that the topic starter is very smart.
There is a Course class with a section variable - what is the class variable? Okay, let's say it's an instance field.
...is a regular string... For example, 2R - our strings can do without quotes.
The regular expression:... is a very good expression, no fools around.
...sheets in a common sheet divided into subsheets again... - so it is necessary to flatly divide or nested?
...split based on regular expression - translate?
In Russian, this question is about the following: there are a bunch of objects, each with attributes: .abbr - no matter what, .section - of the form "2R"
It is necessary to group objects according to the feature (.abbr, suffix(.section))
The solution is obvious:
for obj in inlist:
result[obj.abbr, суффикс(.section)].append(obj) # это если без вложенности
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question