def _equal_chance_permutation(objs, field='albumartist'):
 key = attrgetter(field)
 objs.sort(key=key)
 objs_by_artists = {}
 for (artist, v) in groupby(objs, key):
  objs_by_artists[artist] = list(v)
 while objs_by_artists:
  artist = random.choice(list(objs_by_artists.keys()))
  objs_from_artist = objs_by_artists[artist]
  i = random.randint(0, (len(objs_from_artist) - 1))
  (yield objs_from_artist.pop(i))
  if (not objs_from_artist):
   del objs_by_artists[artist]

def _equal_chance_permutation ( objs , field = 'albumartist' ) : key = attrgetter ( field ) objs . sort ( key = key ) objs_by_artists = { } for ( artist , v ) in groupby ( objs , key ) : objs_by_artists [ artist ] = list ( v ) while objs_by_artists : artist = random . choice ( list ( objs_by_artists . keys ( ) ) ) objs_from_artist = objs_by_artists [ artist ] i = random . randint ( 0 , ( len ( objs_from_artist ) - 1 ) ) ( yield objs_from_artist . pop ( i ) ) if ( not objs_from_artist ) : del objs_by_artists [ artist ]
generate a permutation of the objects where every group with equal values for field have an equal chance of appearing in any given position .