Determine if a list contains a specific number of an item - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Determine if a list contains a specific number of an item (/thread-882.html) |
Determine if a list contains a specific number of an item - flannel_man - Nov-11-2016 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? RE: Determine if a list contains a specific number of an item - micseydel - Nov-11-2016 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? RE: Determine if a list contains a specific number of an item - flannel_man - Nov-12-2016 (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. RE: Determine if a list contains a specific number of an item - micseydel - Nov-12-2016 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. |