3

So lets say I'm playing Go Fish (that's totally what I'm coding btw), and I want to check for pairs with a function. There are a lot of brute force ways it could be done but the way that I think is possible that I'm not sure how to execute is to test for multiple occurrences of the same substring.

Example: Player_hand = ['Ace of Spades', 'Nine of Diamonds', 'Ace of Diamonds']

Now I was trying to use if Player_hand.find('Ace')==2 and that only tests for strings that are in their entirety 'Ace'.

TLDR: I want to check if a substring appears multiple times in a list full of strings. Thanks for any help in advance!

EDIT: This question is not a duplicate of this question

Finding multiple occurrences of a string within a string in Python

This question shows how to find multiple occurrences within a single string, as well as how to find multiple occurrences of whole strings within a list. I am looking to find multiple occurrences of a substring within a list full of strings.

Community
  • 1
  • 1
Bear99
  • 33
  • 4
  • Wow the community is great on here, what a fast response. I don't think it is a duplicate actually. The .find usage is explained as an answer to that one, and it can be used within one single string, or a list of strings (if its looking for one of the whole strings in that list). But I am looking for multiple iterations of a substring within a list full of strings. – Bear99 Jan 18 '17 at 23:13

2 Answers2

4

You can use in for each string in the list:

>>> player_hand = ['Ace of Spades', 'Nine of Diamonds', 'Ace of Diamonds']
>>> sum(1 for x in player_hand if 'Ace' in x)
2

This uses a generator expression to go through the list that gives you a 1 if Ace is in a string of the list. Summing up the ones gives you the total count of strings that contain Ace.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • This does answer my question, but Prune's answer below is also a completely valid answer. Thanks so much. – Bear99 Jan 18 '17 at 23:35
2

This seems to be an "X-Y" problem -- you have a higher-level problem, but asking about the details.

I recommend that you pre-process your input to get rid of unused details. Is there any particular reason that you care about the suits? If not, dump them. If so, keep them ... but code the cards for your internal convenience. For instance, represent the above hand as a list of rank-suit pairs:

player_hand = ["AS", "9D", "AD"]

If needed, you can gather like cards by sorting the list. If not, simply use the count function on the first element to see how many of each item you have. You can do something like

card_rank = "A23456789TJQK"
rank_count = [player_hand.count(char) for char in card_rank]

Also, this generalizes nicely if your next assignment is to program a more complex card game.

Does that get you moving?

UPDATE PER OP COMMENT

player_hand = ["AS", "9D", "AD"]
rank_hand = [card[0] for card in player_hand]
card_rank = "A23456789TJQK"
rank_count = [rank_hand.count(char) for char in card_rank]
print rank_count

Output:

[2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
Prune
  • 76,765
  • 14
  • 60
  • 81
  • Well I think I need the suits. Both to display to the player when he looks at his hand (user-friendliness) and to differentiate the different cards. there can't be just four strings named 'Ace' in the list can there? So I used suits. I'm also not sure what your answer entails; would you mind showing the output for rank_count? Not sure how I could make use of that either without maybe a dictionary. Maybe I don't know what i'm talking about though. – Bear99 Jan 18 '17 at 23:25
  • You *do* know what you're doing. A dictionary is a great translation device between your internal notation and the external display. Even if you don't strip it down to my two-letter form, I recommend that you split the cards into rank-suit pairs; re-insert "of" when you print. – Prune Jan 18 '17 at 23:29
  • That's amazing, thank you so much. Is there any way to have it check for words like that? I really want to keep the Player_hand with the format rank of suit in order to avoid having another variable, and in doing so could you pull from a list similar to your rank_hand but something that would output a 2 in that first spot if the full word 'Ace' appeared twice? Thanks for the vote of confidence as well :) – Bear99 Jan 18 '17 at 23:43
  • Yes, it works just as well for words. In Python, those individual letters are simple words of length 1. Go ahead -- try it out with words instead. You can convert with **" of ".split(player_hand[i])** on your original version of **player_hand** -- that's using " of " as the separator. – Prune Jan 19 '17 at 00:20