python - Why does my variable only return one value outside of a function? -
apologies if obvious, function creates list. printing variable of list, inside function returns list. printing global variable outside function returns first value? here 2 different versions of code:
printing inside formula works:
from itertools import chain, combinations noise_1 = "somerandomvalue" min_var_length = 8 max_var_length = 16 #creates powerset of input value, taking account min , max lengths def powerset(iterable): global powerset_list s = list(iterable) x = list(chain.from_iterable(combinations(s, r) r in range(len(s)+1))) x_string = list(x) items in x_string: if len(items) > min_var_length: powerset_list = items print(powerset_list) powerset(noise_1) and returns powerset of noise_1
however code returns 1 value, though printing same global variable:
from itertools import chain, combinations noise_1 = "somerandomvalue" min_var_length = 8 max_var_length = 16 #creates powerset of input value, taking account min , max lengths def powerset(iterable): global powerset_list s = list(iterable) x = list(chain.from_iterable(combinations(s, r) r in range(len(s)+1))) x_string = list(x) items in x_string: if len(items) > min_var_length: powerset_list = items powerset(noise_1) print(powerset_list) i'm guessing it's terminating function after 1 value, don't know how change behaviour. thank help! sorry r noob.
so confirm, appending list after loop creates list of of items. print command in function prints loop occur, 1 item saved in variable.
Comments
Post a Comment