insertion sorts...
I don't know this list is the right place for newbie questions. I try to implement insertion sort in pyhton. At first code there is no problem. But the second one ( i code it in the same pattern i think ) doesn't work. Any ideas ? def insertion_sort(aList): for i in range(len(aList)): for j in range(i): if aList[i] < aList[j]: aList.insert(j,aList[i]) del aList[i+1] if __name__ == "__main__": MyList = [7,3,5,19,8,2,9,4,15,6,8,3,19] insertion_sort(MyList) print MyList - def insertion_sort(aList): for iterator in aList: for number in range(aList.index(iterator)): if iterator < number: aList.insert(aList.index(number),iterator) del aList[aList.index(iterator)+1] if __name__ == "__main__": MyList = [7,3,5,19,8,2,9,4,15,6,8,3,19] insertion_sort(MyList) print MyList -- http://mail.python.org/mailman/listinfo/python-list
Re: insertion sorts...
On 24 Haziran, 04:33, Terry Reedy <[EMAIL PROTECTED]> wrote: > Matimus wrote: > > May I suggest you look into using `enumerate`: > > for i, val in enumerate([4,5,6]): > > ... print i, val > > ... > > 0 4 > > 1 5 > > 2 6 > > > It allows you to get the index and the value at the same time, which > > should eliminate the need for `aList.index`. > > I thought of suggesting that, but indirectly iterating over a list that > is being modified with enumerate has the same problem as directly > interating over the same changing list. Thanks for all answers. At the end i ve only one point. If a decide to copy list to iterate when will i have to do this ? Before the iteration ? And then iterate through one list and change value of the other ? -- http://mail.python.org/mailman/listinfo/python-list
Re: insertion sorts...
On 25 Haziran, 17:44, MRAB <[EMAIL PROTECTED]> wrote: > On Jun 25, 11:37 am, "A.T.Hofkamp" <[EMAIL PROTECTED]> wrote: > > > > > On 2008-06-25, python_newbie <[EMAIL PROTECTED]> wrote: > > > > On 24 Haziran, 04:33, Terry Reedy <[EMAIL PROTECTED]> wrote: > > > Thanks for all answers. At the end i ve only one point. If a decide to > > > copy list to iterate when will i have to do this ? Before the > > > iteration ? And then iterate through one list and change value of the > > > other ? > > > Before starting the iteration would be a good point > > > I usually do in such cases: > > > for x in mylist[:]: > > ... > > > making a copy just before the for loop starts. > > > Lately, I have started avoiding in-place modification of lists. Instead, I > > construct a new list from scratch inside the for-loop, and replace the old > > list > > with the newly constructed list afterwards like: > > > new_list = [] > > for x in mylist: > > > > new_list.append(x) > > > mylist = new_list > > > by appending a different value than the original or by not appending, you > > can > > influence the contents of the new list. > > > I find this solution nicer than in-place modification of an existing list. > > > Sincerely, > > Albert > > And if you were originally doing in-place modification because there > were also other references to the list then you could just do: > > mylist[:] = new_list Thanks again. I see that you use two different syntax for lists "mylist = new_list" and "mylist[:] = new_list" these are same i think ? -- http://mail.python.org/mailman/listinfo/python-list