Python Forum
Determine if a list contains a specific number of an item
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Determine if a list contains a specific number of an item
#1
Hello! I am a complete noob when it comes to programing and have been trying to teach myself python by creating homework problems for myself... I'm also a poker player, so one of my goals is to figure out how to create an odds calculator (mostly by tinkering and figuring out the most efficient way as I go.) So, here is my question:
I have the program create a random poker hand of 7 cards which is a list of lists:
rank=['A','2','3','4','5','6','7','8','9','T','J','Q','K']
suit=['c','d','h','s']
import itertools
deck=list(itertools.product(rank,suit))
import random
card1=(random.choice(deck))
deck.remove(card1)
card2=(random.choice(deck))
deck.remove(card2)
hand=(card1,card2)
print ("hand: ", hand)
card3=(random.choice(deck))
deck.remove(card3)
card4=(random.choice(deck))
deck.remove(card4)
card5=(random.choice(deck))
deck.remove(card5)
flop=(card3,card4,card5)
print ("flop: ", flop)
card6=(random.choice(deck))
deck.remove(card6)
turn=card6
print ("turn: ", turn)
card7=(random.choice(deck))
deck.remove(card7)
river=card7
print ("river: ", river)
The output will be something like this:
[('3', 'd'), ('T', 'c'), ('7', 'h'), ('4', 'h'), ('6', 'c'), ('Q', 'h'), ('J', 'h')]
I then used this to pull out the first item of each list and make a new list:
all7=[card1,card2,card3,card4,card5,card6,card7]
first=[]
iall7=iter(all7)
for (a,b) in iall7:
    first.append(a)
print (first)
Output for the previous result of random cards is:
['3', 'T', '7', '4', '6', 'Q', 'J']
I now want the program to look through this list and determine if exactly two items on the list are equal and return a specific value if they are. (Basically, I want the program to determine if the poker hand contains a pair). How do I do it without having to write an if statement for each 2 item combination individually?
Reply
#2
What do you mean an if statement for each two-item combination? Why can't you just compare them? Can you write a few lines of what you mean as an example?
Reply
#3
(Nov-11-2016, 05:28 PM)micseydel Wrote: What do you mean an if statement for each two-item combination? Why can't you just compare them? Can you write a few lines of what you mean as an example?

if first[0]==first[1]:
        print ('pair')
elif first[0]==first[2]:
        print ('pair')
elif first[0]==first[3]:
        print ('pair')
#etcetera
else:
        print ('no pair')
I would have to write an 6+5+4+3+2+1=21 'if' statements if I did it this way. And this is also only to determine if the hand contains 2 of the same rank.
It's going to be a million lines of code if I put in all the different hand types such as 2 pair, trips, straights, flushes, full houses, etc. this way.
Reply
#4
I think collections.Counter may help.
from collections import Counter
hand = [('3', 'd'), ('2', 'c'), ('2', 'h'), ('3', 'h'), ('3', 'c'), ('Q', 'h'), ('J', 'h')]
counter = Counter(card[0] for card in hand)
counter.most_common()
# -> [('3', 3), ('2', 2), ('Q', 1), ('J', 1)]
With this, you can look for four- and three-of a kind as well as a pair.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list digit into number Voldyy 2 1,496 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  change item in column based on the item before[solved] amdi40 4 1,713 Jan-11-2022, 04:50 PM
Last Post: amdi40
  Displaying list correspond to the column number danlopek14q 9 3,898 Aug-27-2021, 04:32 AM
Last Post: naughtyCat
  How to convert every even number in a list to odd? Bruizeh 4 3,671 Aug-27-2021, 03:04 AM
Last Post: naughtyCat
  Get the biggest number from a two dimensional list rs74 13 3,942 Aug-09-2020, 04:02 PM
Last Post: deanhystad
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,035 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  How can I print the number of unique elements in a list? AnOddGirl 5 3,196 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Determine if string is integer jrodencal 12 4,045 Oct-15-2019, 01:39 PM
Last Post: jefsummers
  Convert string to a specific number of bytes artblinked 1 2,399 Mar-28-2019, 08:43 PM
Last Post: Larz60+
  Multiplying number in a list in an order pythoneer 12 6,479 Mar-23-2018, 08:21 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020