I was wondering if there was a particular method that would allow me to take a list element (["3D"]) and, using a for loop, nest it within another list ([["3D"]]) while avoiding the current type-conversion problem I’m having which results in [["3","D"]].
I’ve included the following for clarity;
hand = ["3D", "4D", "4C", "5D", "JS", "JC"]
from itertools import groupby
def generate_plays(hand):
plays = []
for rank,suit in groupby(hand, lambda f: f[0]):
plays.append(list(suit))
for card in hand:
if card not in plays: #redundant due to list nesting
plays.append(list(card)) #problematic code in question
return plays
output:
[['3D'], ['4D', '4C'], ['5D'], ['JS', 'JC'], ['3', 'D'], ['4', 'D'], ['4', 'C'], ['5', 'D'], ['J', 'S'], ['J', 'C']]
expected output:
[['3D'], ['4D', '4C'], ['5D'], ['JS', 'JC'], ['4D'], ['4C'], ['5D'], ['JS'], ['JC']]
Just to reiterate, the aim here is to preserve the concatenated-ness of the card element in the for loop.
Many thanks.
P.S. For those interested, it is a play generator for a card game where single cards and 2+ of a number can be played
1 Answer