Instead of repeating the cycle several times "merge" into one

Customization

I am shooting ads using Scrapy. For some characteristics of the lodging, I iterate over the list items BC

that I get per listing. If the characteristic is on the list, I assign "yes", and if not "no". For example.

for x in BC:                
     if 'Terraza' in x:
          terrace = 'yes'
          break
     else:
          terrace = 'no'    

      

For each yes-no characteristic, I have a copy of the above loop.


Problem

Apart from looping through the elements of the list, I would like to loop over the characteristics themselves. That is, I would like to "combine" all loops for one characteristic into one cycle.

I've tried the following (my actual one bcl

contains multiple items):

found = False
bcl = ['Terraza']

for x in l: # l is a list of strings containing housing characteristics   
    for y in bcl:               
        if y in x:
           y = 'yes'
           found = True
           break
        else:
           y = 'no'    
    if found:
        break

terrace = Terrazza 

      

but this loop does not create a variable Terrazza

. I'm not sure I can solve this with globals.

How do I get this loop to work?

+3


source to share


2 answers


You can use a different approach depending on the desired outcome. In such a case, I tend to use a more functional coding style. I'm not sure if this is what you intend, but I think you could do it like this:

list_characteristics = ['Terraza', "Swimming pool"] # sample ad text - list of strings
bcl = ["test1", "test2", "Terraza", "test3"]  # sample checklist - list of strings 

def check_characteristics(checklist, adlist):    
    list_of_found_items = []
    for characteristic in list_characteristics:
        print("characteristic:", characteristic)
        for item in bcl:
            print("item:", item)
            if item in characteristic:
                list_of_found_items.append(item)
    return list_of_found_items

result_list = check_characteristics(bcl, list_characteristics)
print("This ad has the following characteristics:", result_list)

      

Using the code above, you have a function that takes two lists of strings and lists all the elements it finds. If you want to know if there is at least one of them, you can use this other function as a faster, shorter chaining, way:



list_characteristics = ['Terraza', "Swimming pool"] # ad text - list of strings
bcl = ["test1", "test2", "Terraza", "test3"]  # checklist - list of strings 

def has_any_characteristic(checklist, adlist):    
    for characteristic in list_characteristics:
        for item in bcl:
            if item in characteristic:
                return True
    return False

result = has_any_characteristic(bcl, list_characteristics)
print("This ad has at least one of the wanted characteristics?", result)

      

Seems like a lot of code, but you code it once and then use it whenever you need it, in a clean and readable way, IMHO. The definition of these two functions can even be placed in a separate module that you import wherever needed. So, in the main code, you will only need to use one line to call the function. Each function allows you to answer a simple question in a way that is easy to understand, as represented by the statements print()

in both of the code examples above.

0


source


Your problem is not the merging of loops, but probably a violation from the outer loop. You can exit the top loop by raising a custom exception and then try to catch it. Take a look at this world of code:



bcl = ['Terraza']


class FoundCharacteristicException(Exception):
    pass


for x in li:
    try:
        for y in bcl:
            if y in x:
                raise FoundCharacteristicException
    except FoundCharacteristicException:
        break

      

0


source







All Articles