Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Determine if a list contains a specific number of an item flannel_man Unladen Swallow Posts: 2 Threads: 1 Joined: Nov 2016 Reputation: 0 Likes received: 0 #1 Nov-11-2016, 05:19 PM 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? micseydel Involuntary Spiderweb Collector Posts: 2,251 Threads: 56 Joined: Sep 2016 Reputation: 63 Likes received: 676 #2 Nov-11-2016, 05:28 PM 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? Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more. Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :) flannel_man Unladen Swallow Posts: 2 Threads: 1 Joined: Nov 2016 Reputation: 0 Likes received: 0 #3 Nov-12-2016, 05:19 AM (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. micseydel Involuntary Spiderweb Collector Posts: 2,251 Threads: 56 Joined: Sep 2016 Reputation: 63 Likes received: 676 #4 Nov-12-2016, 04:46 PM 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. Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more. Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :) « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Python Adding +1 to a list item cointained in a dict ElReyZero 1 173 Apr-30-2020, 05:12 AM Last Post: deanhystad How can I print the number of unique elements in a list? AnOddGirl 5 270 Mar-24-2020, 05:47 AM Last Post: AnOddGirl Convert string to a specific number of bytes artblinked 1 449 Mar-28-2019, 08:43 PM Last Post: Larz60+ Multiplying number in a list in an order pythoneer 12 1,929 Mar-23-2018, 08:21 PM Last Post: buran adding a number to the list atux_null 4 1,374 Nov-06-2017, 07:01 PM Last Post: gruntfutuk need help removing an item from a list jhenry 4 1,420 Oct-13-2017, 08:15 AM Last Post: buran Determine Number Fulfilling Three Inputs kython 3 1,740 Mar-13-2017, 12:33 PM Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)