I have a list as follows and I want to shuffle the elements that have the same frequency while keeping the elements with different frequencies in the correct positions. The code below works; however, it is not that nice. I was wondering if anyone had some suggestions e.g. using list comprehension.
pieces_needed = [('a',1),('b',1),('d',1),('c',2),('g',3),('f',5),('z',5)] temp =  new_pieces_needed =  current_freq = 1 for i in pieces_needed: if i == current_freq: temp.append(i) else: random.shuffle(temp) new_pieces_needed += temp temp =  temp.append(i) current_freq = i random.shuffle(temp) new_pieces_needed += temp print(new_pieces_needed)