+ 4
Need help with a code.
Import random List=[2,3,4,5,6,7,8,9,10,"A","J","Q","K"] a=random.choice(list) b=random.choice(list) c=random.choice(list) d=random.choice(list) e=random.choice(list) f=random.choice(list) g=random.choice(list) on_table=[a,b,c,d,e,f,g,] **How to count the number of duplicates(pairs) from the on_table list?** For example if a=4 and d=4 to show 1 pair or if a=5, c=4 , d=5 and g=4 to show 2 pairs. I need an example and a explication(detailed or not). #Working on a poker game.(check my Playing Cards code if you want to see how it looks like)
5 Respuestas
+ 5
Ok. You can try use set.
Set is and i quote "is an unordered collection with no duplicate elements. " --Python3 documentation
Here one example
https://code.sololearn.com/cXQgpj292qnt/?ref=app
+ 5
That is exactly what I needed! Thank you! 😘
+ 4
just to be sure. Do you only want the quantity of duplicates?
Edit: Or Do you want to know who is the pair of duplicates and total quantity.
+ 4
Only the quantity of duplicates.
+ 1
def duplicates(on_table):
counter = []
for card in on_table:
if on_table.count(card) > 1 and card not in counter:
counter.append(card)
return len(counter)