Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51888

Re: Removing matching items from a list?

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Removing matching items from a list?
Date 2013-08-03 17:02 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-9A46B1.17024503082013@news.panix.com> (permalink)
References <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com>

Show all headers | View raw


In article <6c0bdea5-23bd-4854-8016-4bf0af3b7d0e@googlegroups.com>,
 kevin4fong@gmail.com wrote:

> let's say there is a list:
> 
> pHands[0] = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd']

I assume this is a card game, and these are cards (ad = Ace of Diamonds, 
etc).

> 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

I'm not quite sure how to parse that, but I think what you're saying 
(using the deck of cards model) is you want to find all the sets of 4 
cards of the same rank.  In the example above, you've got all four aces 
(which, in the wrong saloon, can get you shot).

I think I would do something like:

-----------------------------------------
import collections

def remove_four_of_a_kind(hand):
    ranks = [card[0] for card in hand]
    counts = collections.Counter(ranks)
    fours = [rank for rank, count in counts.items() if count == 4]
    new_hand = [card for card in hand if card[0] not in fours]
    return new_hand

hand = ['ad', 'ac', 'as', 'ah', '7d', '8s', '9d', 'td', 'js', 'jd']
print remove_four_of_a_kind(hand)
-----------------------------------------

$ python cards.py
['7d', '8s', '9d', 'td', 'js', 'jd']

I just gave a class yesterday where we covered list comprehensions and 
Counters, so maybe I just have comprehensions on the brain today :-)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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