def migRing(populations, k, selection, replacement=None, migarray=None):
 nbr_demes = len(populations)
 if (migarray is None):
  migarray = (range(1, nbr_demes) + [0])
 immigrants = [[] for i in xrange(nbr_demes)]
 emigrants = [[] for i in xrange(nbr_demes)]
 for from_deme in xrange(nbr_demes):
  emigrants[from_deme].extend(selection(populations[from_deme], k))
  if (replacement is None):
   immigrants[from_deme] = emigrants[from_deme]
  else:
   immigrants[from_deme].extend(replacement(populations[from_deme], k))
 for (from_deme, to_deme) in enumerate(migarray):
  for (i, immigrant) in enumerate(immigrants[to_deme]):
   indx = populations[to_deme].index(immigrant)
   populations[to_deme][indx] = emigrants[from_deme][i]

def migRing ( populations , k , selection , replacement = None , migarray = None ) : nbr_demes = len ( populations ) if ( migarray is None ) : migarray = ( range ( 1 , nbr_demes ) + [ 0 ] ) immigrants = [ [ ] for i in xrange ( nbr_demes ) ] emigrants = [ [ ] for i in xrange ( nbr_demes ) ] for from_deme in xrange ( nbr_demes ) : emigrants [ from_deme ] . extend ( selection ( populations [ from_deme ] , k ) ) if ( replacement is None ) : immigrants [ from_deme ] = emigrants [ from_deme ] else : immigrants [ from_deme ] . extend ( replacement ( populations [ from_deme ] , k ) ) for ( from_deme , to_deme ) in enumerate ( migarray ) : for ( i , immigrant ) in enumerate ( immigrants [ to_deme ] ) : indx = populations [ to_deme ] . index ( immigrant ) populations [ to_deme ] [ indx ] = emigrants [ from_deme ] [ i ]
perform a ring migration between the *populations* .