A
A
ac130kz2017-08-13 15:17:16
Python
ac130kz, 2017-08-13 15:17:16

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())

The question is to divide the created sheets in the general sheet inlist into subsheets again, but already on the basis of the regular expression indicated above, and to the section variable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2017-08-13
@ac130kz

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) # это если без вложенности

Walk some more.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question