1

The list comprehension below achieves the desired result/output:

  1. Each tuple in the list is indexed sequentially
  2. The names are grouped together by suit
  3. The names are in a consistent order

Code (deck of cards toy example):

Suits = ["Hearts"]*3 +["Diamonds"]*3
Names = ["Two","Ten","King"]

z = [(i,Names[(i-1)%3],Suits[((i-1)%6)]) for i in range(1,7)]

Output z:

[(1, 'Two', 'Hearts'),
 (2, 'Ten', 'Hearts'),
 (3, 'King', 'Hearts'),
 (4, 'Two', 'Diamonds'),
 (5, 'Ten', 'Diamonds'),
 (6, 'King', 'Diamonds')]

It works because the original Suits list was changed from ["Hearts","Diamonds"] to["Hearts"]*3 +["Diamonds"]*3.

This feels contrived; although it works, I'm looking for a better solution.

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
Cal P
  • 19
  • 1
  • Python naming convention is lower_case_with_underscores for variables and functions, so `suits`, `names` instead of `Suits`, `Names` – smci Feb 12 '22 at 02:07
  • Kraigolas, Smci, thank you for the edits! – Cal P Feb 12 '22 at 02:14
  • If you want to use `pandas` package, there is a neat idiom using CROSS-JOIN: [Cartesian product in pandas](https://stackoverflow.com/a/13270110/202229) – smci Feb 12 '22 at 02:19

3 Answers3

3

Using enumerate and itertools.product:

from itertools import product

ranks = ["Two", "Ten", "King"]
suits = ["Hearts", "Diamonds"]
z = [(i, r, s) for i, (s, r) in enumerate(product(suits, ranks), start=1)]

Output:

[(1, 'Two', 'Hearts'),
 (2, 'Ten', 'Hearts'),
 (3, 'King', 'Hearts'),
 (4, 'Two', 'Diamonds'),
 (5, 'Ten', 'Diamonds'),
 (6, 'King', 'Diamonds')]
kaya3
  • 47,440
  • 4
  • 68
  • 97
3

You can use itertools.product and enumerate:

from itertools import product

suits = ["Hearts", "Diamonds"]
sames = ["Two","Ten","King"]

cards = [(index, suit, rank) for index, (suit, rank) in enumerate(product(sames, suits), start=1)]

print(cards)
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
-1

you could use a two level nested comprehension with enumerate to get numbering:

Suits = ["Hearts","Diamonds"]
Names = ["Two","Ten","King"]


r = [(j+i,n,s) for j,s in enumerate(Suits) 
               for i,n in enumerate(Names,j*len(Suits)+1) ]

print(*r,sep="\n")
(1, 'Two', 'Hearts')
(2, 'Ten', 'Hearts')
(3, 'King', 'Hearts')
(4, 'Two', 'Diamonds')
(5, 'Ten', 'Diamonds')
(6, 'King', 'Diamonds')
Alain T.
  • 40,517
  • 4
  • 31
  • 51