how to shuffle a list correctly in python


I have this code that shuffles a list. I first split it into two lists because I have a interleave function that interleaves 2 lists:

def shuffle(xs, n=1): il=list() if len(xs)%2==0: stop=int(len(xs)//2) a=xs[:stop] b=xs[stop:] print(a) print(b) else: stop=int(len(xs)//2) a=xs[:stop] b=xs[stop:] print(a) print(b) if n>0: for i in range(n): shuffle=interleave(a,b) else: return return shuffle

and when I test it:

>>> shuffle([1,2,3,4,5,6,7],1) [1, 2, 3] [4, 5, 6, 7] 1 [7] [7, 4] [1, 4, 2, 5, 3, 6, 7, 4]

What is 4 in the list twice and why is it printing 1, [7], 7,4]??


def interleave(xs,ys): a=xs b=ys minlength=[len(a),len(b)] extralist= list() interleave= list() for i in range((minval(minlength))): pair=a[i],b[i] interleave.append(pair) flat=flatten(interleave) c=a+b if len(b)>len(a): remainder=len(b)-len(a) for j in range(remainder,-1,-1): extra=b[-j] extralist.append(extra) if len(a)>len(b): remainder=len(a)-len(b) for j in range(remainder,-1,-1): extra=a[-j] extralist.append(extra) del extralist[-1] final=flat+extralist return final


Why not just use the standard library?

>>> from random import shuffle >>> l = list(range(1,20)) >>> l [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> shuffle(l) >>> l [17, 15, 9, 13, 19, 7, 10, 18, 5, 1, 12, 3, 2, 16, 4, 14, 8, 6, 11] >>>


Assuming you are wanting to interleave the lists, you can write a simple recursive function to do it n number of times. The one thing with interleaving the list is that the first and last characters will always be the same, I believe.

def shuffle(lst, num): ''' lst - is a list num - is the amount of times to shuffle ''' def interleave(lst1,lst2): ''' lst1 and lst2 - are lists to be interleaved together ''' if not lst1: return lst2 elif not lst2: return lst1 return lst1[0:1] + interleave(lst2, lst1[1:]) while num > 0: lst = interleave(lst[:len(lst)/2], lst[len(lst)/2:]) print lst num -= 1 return lst


  • video normalization with ffmpeg
  • Printing index and element of array
  • jQuery Validate plugin not working
  • how to return an object (from a MySQL temporary table) into a Coldfusion stored procedure?
  • Validation plugin is not validating all fields
  • Fastest way to clamp a real (fixed/floating point) value?
  • Configure Ajax request made by Kendo to support cross domain ajax request
  • How to select elements like question1, question2, question3,… in JQuery form validation plug-in?
  • Several custom validate rules in Jquery Validate plugin
  • Label transition issue
  • MysqlError: Duplicate entry '1-5' for key 'PRIMARY' on insert unsure of how
  • Aurelia validation: applying some rule on change and some on blur on same property
  • Making a basic web scrapper in Python with only built in libraries - Python newbie
  • JqueryUI Autocomplete : only one character is displayed per list item
  • Shuffling an Array in Android
  • JQuery Auto-Complete: How do I handle modifications?
  • Excel's Macro-Recorder usage
  • Can I check if a recipient has an automatic reply before I send an email?
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • How to handle AllServersUnavailable Exception
  • JSON with duplicate key names losing information when parsed
  • VBA Convert delimiter text file to Excel
  • Jquery - Jquery Wysiwyg return html as a string
  • align graphs with different xlab
  • Return words with double consecutive letters
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?