Hello and thanks for looking at my question! I have read on the documentations for python and the top rated question for itertools.groupby( ) in python. But I'm still confused as to how this function actually works. Can someone walk me through the for rank, group in groupby (hand , lambda card: card [1])
iteration? Especially the lambda card: card[1]
part. From my understanding it is just returning card[1] but there is no variable with card. Also, for rank, group
is it because there are 2 values for each index of hand e.g. the first card is '3S' hence there is 3 and S for its values? Would there need to have a third variable if the say each index of hand were changed to 3 values? E.g. '3SH'? Sorry if this seems like a question that was already answered, I really couldn't understand it even in simple terms...
hand = ['3S', '3D', '3H', '4D']
sorted_hand_by_suit = []
for rank, group in groupby (hand , lambda card: card [1]):
sorted_hand_by_suit.append(list(group))
return sorted_hand_by_suit
print sorted_hand_by_suit # [['3S'],['3D', '4D'], [3H]]