Clement Ros
Clement Ros

Reputation: 329

Remove value of multiple list

I have a python code. My subject is the following :

I have a french weather article. I extracted location and i want to keep only location with preposition ["de", "à", "d"].

For one article of my dataset i have an index out of range error. This error if generated by my localisation selection function.

def loc_selection(lieu, prep, lon, lat): 
    i=0
    while True:
        if prep[i] not in ["de", "à", "d'"]:
            lieu.pop(i)
            prep.pop(i)
            lon.pop(i)
            lat.pop(i)
            i = i - 1
        i = i + 1
        if i >= (len(prep)):
            break

    return lieu, prep, lon, lat

I have 4 list lieu, prep, lon, lat. I want to remove all data in prep field that is not a correct preposition. I also want to remove corresponding data in other lists.

I'm not sure my method is the best. Feel free to apply your own method.

Thank you for your help.

Upvotes: 0

Views: 42

Answers (2)

Jab
Jab

Reputation: 27485

Im assuming you’re looking to remove elements from all lists keeping order. frozenset and set can be of use:

def remove_from_all(remove, *iterables):
    rem = set(remove)
    return (list(it - rem) for it in map(frozenset, iterables))

remove = [34, 18, 24]
lists = [1,3,18], [34, 13, 5], [64, 24, 10]
print(list(remove_from_all(remove, *lists)))

This prints:

[[1, 3], [13, 5], [64, 10]]

I just used random integers for example so you can see what I’d does as you didn’t provide enough detail on input.

Note: this will also remove duplicates, so you can use itertools.iterfalse to keep all elements and order without items in remove:

def remove_from_all(remove, *iterables):
    rem = set(remove).__contains__
    return (list(filterfalse(rem, it)) for it in iterables)

Upvotes: 0

Patrick Haugh
Patrick Haugh

Reputation: 60974

It looks like zipping the lists together and iterating through them in parallel is a better fit for what you want to do. The following yields (lieu, prep, long, lat) tuples with one of the prefixes:

def loc_selector(lieus, preps, longs, lats, prefixes=("de", "à", "d'")):
    for lieu, prep, long, lat in zip(lieus, preps, longs, lats):
        if prep in prefixes:
            yield lieu, prep, long, lat 

Then you can use zip again on that generator to get them back into lists:

lieus, preps, longs, lats = zip(*loc_selector(lieus, preps, longs, lats))

zip will stop automatically at the end of the shortest list.

Upvotes: 1

Related Questions