python - list wasn't removed correctly using list.remove() method -
binary watch trying valid set through following approach. all_comb
still includes result such [8, 4, 2, 1, 0.32, 0.16, 0.08, 0.04, 0.02, 0.01]
it seems check function removes part of list contains [8,4]
, [0.32, 0.16, 0.08, 0.04]
i've been stuck on while. appreciated.
`
upperlst = [8, 4, 2, 1] lowerlst = [0.32, 0.16, 0.08, 0.04, 0.02, 0.01] lst = upperlst + lowerlst
`
`def all_com(lst, num): new_lst = [] l in range(len(lst) + 1): subset in itertools.combinations(lst, l): new_lst.append(list(subset)) return new_lst def remove(lst): """check if lst valid combinations""" upper = all(x in lst x in [4, 8]) lower = all(x in lst x in [0.32, 0.16, 0.08, 0.04]) return upper or lower or false def check(lst): """check combinations valid""" l in lst: print("not removed lst", l) if remove(l): print('removed list: ', l) lst.remove(l)
`
if loop through list modify it, mess order of iteration. ran following example on interpreter:
list = range(10) val in list: print val list.remove(val)
the output 0 2 4 6 8, , not 0,1,2,3,4,5,6,7,8 .
you fix appending valid combinations list , returning list, don't modify original list.
Comments
Post a Comment