How to append an element in a list a declared in global , while list.append() statement is inside a function

corrected code for query , both var and result are global variables which are required to be accessed inside a function

import copy
result = []
var = 'second list empty'

def permute(arr1,arr2):    
    if(len(arr2) <= 0):
        global result.append(var)
        return
    stx = "".join(arr1)
    for x in arr2:
       var = stx + x
       arr3 = copy.deepcopy(arr2)
       arr3.remove(x)
       permute(var.split(),arr3)   

Answers


Remove the word global from your append statement.

You have:

global result.append(var)

It should be:

result.append(var)

You only need the global keyword when you`re reassigning a global variable inside the function, and you use it just to declare the variable global; you can't do anything else with it in that statement.


Need Your Help

How to use SVM in Matlab?

matlab machine-learning classification svm

I am new to Matlab. Is there any sample code for classifying some data (with 41 features) with a SVM and then visualize the result? I want to classify a data set (which has five classes) using the ...