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


Messages In This Thread
Determine if a list contains a specific number of an item - by flannel_man - Nov-11-2016, 05:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  list digit into number Voldyy 2 1,534 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  change item in column based on the item before[solved] amdi40 4 1,764 Jan-11-2022, 04:50 PM
Last Post: amdi40
  Displaying list correspond to the column number danlopek14q 9 3,960 Aug-27-2021, 04:32 AM
Last Post: naughtyCat
  How to convert every even number in a list to odd? Bruizeh 4 3,756 Aug-27-2021, 03:04 AM
Last Post: naughtyCat
  Get the biggest number from a two dimensional list rs74 13 4,039 Aug-09-2020, 04:02 PM
Last Post: deanhystad
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,068 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  How can I print the number of unique elements in a list? AnOddGirl 5 3,264 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Determine if string is integer jrodencal 12 4,130 Oct-15-2019, 01:39 PM
Last Post: jefsummers
  Convert string to a specific number of bytes artblinked 1 2,428 Mar-28-2019, 08:43 PM
Last Post: Larz60+
  Multiplying number in a list in an order pythoneer 12 6,597 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