Removing matching items from a list?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Aug 3 20:40:51 EDT 2013


On Sat, 03 Aug 2013 17:29:28 -0700, kevin4fong wrote:

> Would you also happen to know how I could go about setting up a list
> that keeps track of the removed sets?
> 
> For example, if there were 4 a's removed, that would be one set. And the
> list would be:
> 
> ['a']
> 
> but if there was also 4j's in addition to the a's. It would be:
> 
> ['a', 'j']
> 
> And so forth....


How would *you* do it in person? Start with that, then turn it into 
Python code.

For example, you might count the cards first, as in my previous post, 
then look at each counter. If the counter is four (or greater), add it to 
a list of cards to be removed.

# counter is a dict like {'a': 4, '7': 1} as per previous post
cards_to_remove = []
for card in counter:
    if counter[card] >= 4:
        cards_to_remove.append(card)


Or you could do this at the same time as you build the counter. I leave 
it to you to work out how to do that.

The important thing is, you have to start by asking the question, "How 
would I do this task?" before you can write Python code for it. 



-- 
Steven



More information about the Python-list mailing list