You could also try this, which is a bit more pythonic, also faster. def re_sort(to_sort): > # we need those zeroes so we check how many of them we have > nr_of_zeroes = to_sort.count(0) > # list comprehension that gets rid of zeroes > filtered = [item for item in to_sort if item != 0] > # we append together the filtered part and inplace add a list of > zeroes of length nr_of_zeroes > return filtered + [0]*nr_of_zeroes
While this has time complexity of O(n), yours is O(n^2), so the longer your list gets, the worse your performance gets. On Sunday, November 5, 2017 at 3:07:32 PM UTC+1, Virbhadra Gupta wrote: > > > > i was trying to sort list by only 0 and other Numbers like [2,0,0,4] to > [2,4,0,0] > i come up with this code but in this code variable list is changing. i am > unable to understand why ? > > list = [4,0,0,4] > > def re_sort(list): > count=0 > tmp_list = list > print list > for i in list: > if not i: > tmp_list.remove(i) > count+=1 > print list > for n in range(count): > tmp_list.append(0) > return tmp_list > re_sort(list) > > ************************ > re_sort(list) > [4, 0, 0, 4] > [4, 0, 4] > # Result: [4, 0, 4, 0] # > -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/700827e3-cb90-4106-8da4-350e08096dbd%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
