--
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e4b95d9e-bda4-4499-9d94-860be6aaaf5b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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
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_zeroesWhile 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=0tmp_list = listprint listfor i in list:if not i:tmp_list.remove(i)count+=1print listfor n in range(count):tmp_list.append(0)return tmp_listre_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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/700827e3-cb90-4106-8da4-350e08096dbd%40googlegroups.com.
Ooo, I’ve been waiting for a reason to use this!
# import sys
# maxint = sys.maxint
by_nonzero = lambda i: i or float("inf")
l = [4,0,8,0,0,2,1,-5]
l.sort(key=by_nonzero)
print l
# [-5, 1, 2, 4, 8, 0, 0, 0]
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
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.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1k7yVorfG_sbf2Pj7%3DG4qo5X-A7vhOO0DQZseu%3DxSGjA%40mail.gmail.com.To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Though this is probably not particularly useful for the problem the OP’s was having, which is that lists in Python are mutable.
Meaning that, when you pass a list into a function, changes you make to it are also made to the original list you passed in. If you’re familiar with C++, then you can think of the list you pass as being passed by reference.
def append(a, b):
a.append(b)
my_list = ["Hello"]
append(my_list, "World")
print(" ".join(my_list))
# Hello World
In order to pass a list, and not modify it, you can do what Alok suggested, which is to make a copy of the list inside of the function.
def append(a, b):
copy = a[:]
copy.append(b)
my_list = ["Hello"]
append(my_list, "World")
print(" ".join(my_list))
# Hello
Ooo, I’ve been waiting for a reason to use this!
# import sys # maxint = sys.maxint by_nonzero = lambda i: i or float("inf") l = [4,0,8,0,0,2,1,-5] l.sort(key=by_nonzero)print l # [-5, 1, 2, 4, 8, 0, 0, 0]
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
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.
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1k7yVorfG_sbf2Pj7%3DG4qo5X-A7vhOO0DQZseu%3DxSGjA%40mail.gmail.com.
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOA8PD%3DAMFtoyMvoQ5J3BT69GWxJotk%3DPk0yG0dh_FvCWw%40mail.gmail.com.
Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier.
Yes:
def f(x): return 2*x
No:
f = lambda x: 2*x
The first form means that the name of the resulting function object is specifically 'f' instead of the generic '<lambda>'. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression)
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.
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.
--
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 python_inside_maya+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1k7yVorfG_sbf2Pj7%3DG4qo5X-A7vhOO0DQZseu%3DxSGjA%40mail.gmail.com.
--
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOA8PD%3DAMFtoyMvoQ5J3BT69GWxJotk%3DPk0yG0dh_FvCWw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Being really picky, please do not use lambda in a variable (PEP8) -Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier.
Yes:
def f(x): return 2*xNo:
f = lambda x: 2*xThe first form means that the name of the resulting function object is specifically 'f' instead of the generic '<lambda>'. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression)
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
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.
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1k7yVorfG_sbf2Pj7%3DG4qo5X-A7vhOO0DQZseu%3DxSGjA%40mail.gmail.com.
--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOA8PD%3DAMFtoyMvoQ5J3BT69GWxJotk%3DPk0yG0dh_FvCWw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
----
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMSrJKG_Vuj5PxmjScOhc_Ly%3Ds2B9KLpBLbY_wy087h6aA%40mail.gmail.com.
Also don’t declare functions on a single line (PEP8, E704) ;)
# No
def f(x): return 2*x
# Yes
def f(x):
return 2*x
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
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.
--
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1k7yVorfG_sbf2Pj7%3DG4qo5X-A7vhOO0DQZseu%3DxSGjA%40mail.gmail.com.
--
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOA8PD%3DAMFtoyMvoQ5J3BT69GWxJotk%3DPk0yG0dh_FvCWw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
----
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 python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPaTLMSrJKG_Vuj5PxmjScOhc_Ly%3Ds2B9KLpBLbY_wy087h6aA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1L_Wvsf0bUajceGp6AgBiA6exQnOUZOy-hx-ZToz9Ffg%40mail.gmail.com.To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Is the original solution suggested to be O(n^2) because of the nested remove(i) call within the loop?
Would that have a logarithmic complexity instead because the list becomes shorter everytime it finds a 0 and removes it?
n + (n-1) + (n-2) ... +1)python -m timeit "listerine = [0]*100;listerine.remove(0)"python -m timeit "listerine = [0]*100"And then your solution has two O(n) calls (the count and the list comp) so that is O(n) yea?
Then you have the memory complexity of needing to allocate temporary lists for the final nonzero and zero lists.
import sys
maxint = sys.maxint
by_nonzero = lambda i: i or maxint
l = [4,0,8,0,0,2,1,-5]
l.sort(key=by_nonzero)
print l
# [-5, 1, 2, 4, 8, 0, 0, 0]
import string, operator, random
dict_compr = {letter : random.randint(0, 10) for letter in string.ascii_lowercase}
sorted_by_values = sorted(dict_compr.iteritems(), key=operator.itemgetter(1))To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.