Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Python Permutation without built-in function [itertools] for Lists

def permutation(list1):  
   # If the length of list=0 no permuataions possible 
   if len(list1) == 0:  
       return []  
   # If the length of list=1, return that element 
   if len(list1) == 1:  
       return [list1]  
   l = []  
   for i in range(len(list1)):  
       m = list1[i]  
      # Extract list1[i] or m from the list. remlist1 is  
      # remaining list  
       remlist1 = list1[:i] + list1[i+1:]  
      # Generating all permutations where m is first  
      # element  
       for p in permutation(remlist1):  
            l.append([m] + p)  
   return l 
if __name__=="__main__": 
   print(list(permutation([1,2,3,4])))
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #Python #Permutation #function #Lists
ADD COMMENT
Topic
Name
9+8 =