pattern1 = '<span(.*?)</span>' res = re.findall(pattern1, textdata) resultlist = [] for z in range(len(res)): if 'style=font-weight:bold' in res[z]: is_bold = True elif 'uclass_24' in res[z]: is_bold = True elif 'uclass_48' in res[z]: is_bold = True elif 'uclass_72' in res[z]: is_bold = True elif 'uclass_96' in res[z]: is_bold = True else: is_bold = False resultlist.append(is_bold) data['is_bold'] = resultlist resultlist.count(True) 

It is necessary to simplify this code using a loop (where elif)

Closed due to the fact that off-topic participants Enikeyschik , Dmitry Kozlov , freim , Suvitruf ♦ , LFC 8 Mar at 8:58 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- Enikeyschik, Dmitry Kozlov, freim, Suvitruf, LFC
If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

     pattern1 = '<span(.*?)</span>' res = re.findall(pattern1, textdata) resultlist = [] for z in range(len(res)): if 'style=font-weight:bold' in res[z]: is_bold = True elif [ True for i in [ 'uclass_24', 'uclass_48', 'uclass_72', 'uclass_96'] if i in res[z]]: is_bold = True else: is_bold = False resultlist.append(is_bold) data['is_bold'] = resultlist resultlist.count(True) 

    Try this:

     # ... resultlist = [] listClass = ['style=font-weight:bold', 'uclass_24', 'uclass_48', 'uclass_72', 'uclass_96',] for z in range(len(res)): is_bold = False for i in listClass: if i in res[z]: is_bold = True break resultlist.append(is_bold) # ... 
    • that if such 'uclass' is not 4, but many times more, then a regular entry will be time consuming. There uclass goes with step 24, is it possible to write something through a cycle? - Pupkin Putya
    • /////////// @ S. Nick - Pupkin Putya
    • Added, try it - S. Nick