Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51889
| Date | 2013-08-03 22:06 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: Removing matching items from a list? |
| References | <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.171.1375563983.1251.python-list@python.org> (permalink) |
On 03/08/2013 21:12, kevin4fong@gmail.com wrote:
>
>
> Basically, I'm trying to find out how to remove matching items from a list. But there doesn't seem to be any information on how to go about doing this specific function.
>
> For example, what I want is:
>
> let's say there is a list:
>
> pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd']
>
> So up there, my list, which is named pHands[0] has ten items in it.
>
> I'm trying to make a function where a search is initiated into the list and any matching items with a first matching number/letter reaching four are removed
>
> So in the end, ad, ac, as, ah (the four a's) will all be deleted/removed from the list. I need the list to automatically detect if there are four matching first letter/numbers in the items in the list.
>
> The remaining list will be: pHands[0] = ['7d', '8s', '9d', 'td', 'js', 'jd']
>
>>> hands = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd']
>>> from collections import Counter
>>> counts = Counter(hand[0] for hand in hands)
>>> counts
Counter({'a': 4, 'j': 2, 't': 1, '7': 1, '9': 1, '8': 1})
>>> counts.most_common()
[('a', 4), ('j', 2), ('t', 1), ('7', 1), ('9', 1), ('8', 1)]
So there are 4 aces.
>>> hands = [hand for hand in hands if hand[0] != 'a']
>>> hands
['7d', '8s', '9d', 'td', 'js', 'jd']
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Removing matching items from a list? kevin4fong@gmail.com - 2013-08-03 13:12 -0700
Re: Removing matching items from a list? kevin4fong@gmail.com - 2013-08-03 13:16 -0700
Re: Removing matching items from a list? woooee@gmail.com - 2013-08-03 13:27 -0700
Re: Removing matching items from a list? Roy Smith <roy@panix.com> - 2013-08-03 17:02 -0400
Re: Removing matching items from a list? MRAB <python@mrabarnett.plus.com> - 2013-08-03 22:06 +0100
Re: Removing matching items from a list? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-03 22:37 +0000
Re: Removing matching items from a list? Roy Smith <roy@panix.com> - 2013-08-03 19:06 -0400
Re: Removing matching items from a list? Chris Angelico <rosuav@gmail.com> - 2013-08-04 00:35 +0100
Re: Removing matching items from a list? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-04 00:25 +0000
Re: Removing matching items from a list? kevin4fong@gmail.com - 2013-08-03 17:33 -0700
Re: Removing matching items from a list? kevin4fong@gmail.com - 2013-08-03 17:34 -0700
Re: Removing matching items from a list? kevin4fong@gmail.com - 2013-08-03 17:23 -0700
Re: Removing matching items from a list? kevin4fong@gmail.com - 2013-08-03 17:29 -0700
Re: Removing matching items from a list? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-04 00:40 +0000
Re: Removing matching items from a list? kevin4fong@gmail.com - 2013-08-03 17:32 -0700
Re: Removing matching items from a list? kevin4fong@gmail.com - 2013-08-03 17:33 -0700
Re: Removing matching items from a list? Dave Angel <davea@davea.name> - 2013-08-04 01:53 +0000
csiph-web