排序麻烦 [英] Sorting troubles

查看:49
本文介绍了排序麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下快速排序和插入排序的实现:


def qSort(列表):

如果List == []:return []

返回qSort([x为列表中的x [1:]如果x< List [0]])+列表[0:1] + \

qSort( [x for list in [1:]如果x> = List [0]])


def insertSort(List):

for i in range (1,len(List)):

value = List [i]

j = i

而List [j-1]>值和j> 0:

列表[j] =列表[j-1]

j- = 1

列表[j] =价值


现在,quickSort不会修改原始列表,主要是因为

它适用于产品和连接,而不是改动。

然而,插入排序会修改列表。现在,要给出

的结果,应该以这种方式调用它们(A是未分类的列表)

A = qSort(A)

#插入排序不需要这个,

insertSort(A)

我想知道如何修改qSort函数使它

直接影响列表内部

我试过这样做


def qSort(列表):

如果List == []:return []

List = qSort([x for list in [1:]如果x< List [0]])+ List [0:1 ] + \

qSort([x为列表中的x [1:]如果x> = List [0]])

返回列表
,列表会发生变化,但这些更改仍保留在

函数内部,所以一旦它结束,如果不能获得回报,

原始列表保持不变。


如果不是解决方案,我想至少知道为什么它会做什么呢?
。我明白List(上面)只是对

实际数据(列表)的引用,但我没有将数据副本传递给

函数,但实际参考(因此,insertSort确实修改了b $ b)。但是,如何在函数内部更改

对象列表引用?如果我不能修改原始列表,

也许我可以将变量List指向另一个列表?但是函数内的更改

是本地的。对不起,如果这有点令人困惑。

I have the following implementations of quicksort and insertion sort:

def qSort(List):
if List == []: return []
return qSort([x for x in List[1:] if x< List[0]]) + List[0:1] + \
qSort([x for x in List[1:] if x>=List[0]])

def insertSort(List):
for i in range(1,len(List)):
value=List[i]
j=i
while List[j-1]>value and j>0:
List[j]=List[j-1]
j-=1
List[j]=value

Now, the quickSort does not modify the original list, mostly because
it works on products and concatenations, rather than alterations.
The insertion sort, however, does modify the list. Now, to give
results, they should be called in such a way( A is an unsorted list)
A=qSort(A)
# the insertion sort does not require this,
insertSort(A)

I would like to know how can I modify the qSort function such that it
affects the list directly inside
I have tried doing it like this

def qSort(List):
if List == []: return []
List = qSort([x for x in List[1:] if x< List[0]]) + List[0:1] + \
qSort([x for x in List[1:] if x>=List[0]])
return List

while processing, the list changes, but those changes remain inside
the function, so that once it''s over, if nothis catches the return,
the original List remains unchanged.

If not a solution, I would like to at least know why it does what it
does. I so understand that List(above) is only a reference to the
actual data(a list), but I''m not passing a copy of the data to the
function, but the actual reference(hence, insertSort does
modifications). But then how can I change, inside the function, the
object List is referencing to? If I can''t modify the original list,
maybe I can make the variable List point to another list? But changes
inside the function are local. Sorry if this is a bit confusing.

推荐答案

5月14日上午11:05,seyens ... @ yahoo.com写道:
On May 14, 11:05 am, seyens...@yahoo.com wrote:

我有以下快速排序和插入排序的实现:


def qSort(List):

如果List == []:return []

返回qSort([x为List中的x [1:]如果x< List [0]])+ List [0: 1] + \

qSort([x为列表中的x [1:]如果x> = List [0]])


def insertSort(列表):

for i in range(1,len(List)):

value = List [i]

j = i

而List [j-1]>值和j> 0:

List [j] = List [j-1]

j- = 1

列表[j] =值


现在,quickSort不会修改原始列表,主要是因为

它适用于产品和连接,而不是改动。

然而,插入排序确实如此修改列表。现在,要给出

的结果,应该以这种方式调用它们(A是未分类的列表)

A = qSort(A)

#插入排序不需要这个,

insertSort(A)

我想知道如何修改qSort函数使它

直接影响列表内部

我试过这样做


def qSort(列表):

如果List == []:return []

List = qSort([x for list in [1:]如果x< List [0]])+ List [0:1 ] + \

qSort([x为列表中的x [1:]如果x> = List [0]])

返回列表
,列表会发生变化,但这些更改仍保留在

函数内部,所以一旦它结束,如果不能获得回报,

原始列表保持不变。


如果不是解决方案,我想至少知道为什么它会做什么呢?
。我明白List(上面)只是对

实际数据(列表)的引用,但我没有将数据副本传递给

函数,但实际参考(因此,insertSort确实修改了b $ b)。但是,如何在函数内部更改

对象列表引用?如果我不能修改原始列表,

也许我可以将变量List指向另一个列表?但是函数内的更改

是本地的。对不起,如果这有点令人困惑。
I have the following implementations of quicksort and insertion sort:

def qSort(List):
if List == []: return []
return qSort([x for x in List[1:] if x< List[0]]) + List[0:1] + \
qSort([x for x in List[1:] if x>=List[0]])

def insertSort(List):
for i in range(1,len(List)):
value=List[i]
j=i
while List[j-1]>value and j>0:
List[j]=List[j-1]
j-=1
List[j]=value

Now, the quickSort does not modify the original list, mostly because
it works on products and concatenations, rather than alterations.
The insertion sort, however, does modify the list. Now, to give
results, they should be called in such a way( A is an unsorted list)
A=qSort(A)
# the insertion sort does not require this,
insertSort(A)

I would like to know how can I modify the qSort function such that it
affects the list directly inside
I have tried doing it like this

def qSort(List):
if List == []: return []
List = qSort([x for x in List[1:] if x< List[0]]) + List[0:1] + \
qSort([x for x in List[1:] if x>=List[0]])
return List

while processing, the list changes, but those changes remain inside
the function, so that once it''s over, if nothis catches the return,
the original List remains unchanged.

If not a solution, I would like to at least know why it does what it
does. I so understand that List(above) is only a reference to the
actual data(a list), but I''m not passing a copy of the data to the
function, but the actual reference(hence, insertSort does
modifications). But then how can I change, inside the function, the
object List is referencing to? If I can''t modify the original list,
maybe I can make the variable List point to another list? But changes
inside the function are local. Sorry if this is a bit confusing.



事情是[在List [1:]中的x为x]是创建的全新列表

迭代旧的。

怎么样

qSortHelp(列表):

newlist = qSort(List)

for i,val in enumerate(newlist):

List [i] = val

你必须再迭代一次,但是这个列表在

的地方排序。

HTH,

Tom

The thing is that [x for x in List[1:]...] is a brand new list created
by iterating over the old one.
How about:
qSortHelp(List):
newlist = qSort(List)
for i, val in enumerate(newlist):
List[i] = val
You have to iterate over one more time, but this sorts the list in
place.
HTH,
Tom


5月14日下午12:05,seyens ... @ yahoo.com写道:
On May 14, 12:05 pm, seyens...@yahoo.com wrote:

我有以下快速排序和插入排序的实现:


def qSort(列表):

如果List == []:return []

return qSort([x for x在List [1:]中如果x< List [0]])+ List [0:1] + \

qSort(如果x> = List,[x代表List [1:]中的x [0]])


def insertSort(List):

for i in range(1,len(List)):

value = List [i]

j = i

而List [j-1]>值和j> 0:

List [j] = List [j-1]

j- = 1

列表[j] =值


现在,quickSort不会修改原始列表,主要是因为

它适用于产品和连接,而不是更改。

然而,插入排序会修改列表。现在,要给出

的结果,应该以这种方式调用它们(A是未分类的列表)

A = qSort(A)

#插入排序不需要这个,

insertSort(A)

我想知道如何修改qSort函数使它

直接影响列表内部

我试过这样做


def qSort(列表):

如果List == []:return []

List = qSort([x for list in [1:]如果x< List [0]])+ List [0:1 ] + \

qSort([x为列表中的x [1:]如果x> = List [0]])

返回列表
,列表会发生变化,但这些更改仍保留在

函数内部,所以一旦它结束,如果不能获得回报,

原始列表保持不变。


如果不是解决方案,我想至少知道为什么它会做什么呢?
。我明白List(上面)只是对

实际数据(列表)的引用,但我没有将数据副本传递给

函数,但实际参考(因此,insertSort确实修改了b $ b)。但是,如何在函数内部更改

对象列表引用?如果我不能修改原始列表,

也许我可以将变量List指向另一个列表?但是函数内的更改

是本地的。对不起,如果这有点令人困惑。
I have the following implementations of quicksort and insertion sort:

def qSort(List):
if List == []: return []
return qSort([x for x in List[1:] if x< List[0]]) + List[0:1] + \
qSort([x for x in List[1:] if x>=List[0]])

def insertSort(List):
for i in range(1,len(List)):
value=List[i]
j=i
while List[j-1]>value and j>0:
List[j]=List[j-1]
j-=1
List[j]=value

Now, the quickSort does not modify the original list, mostly because
it works on products and concatenations, rather than alterations.
The insertion sort, however, does modify the list. Now, to give
results, they should be called in such a way( A is an unsorted list)
A=qSort(A)
# the insertion sort does not require this,
insertSort(A)

I would like to know how can I modify the qSort function such that it
affects the list directly inside
I have tried doing it like this

def qSort(List):
if List == []: return []
List = qSort([x for x in List[1:] if x< List[0]]) + List[0:1] + \
qSort([x for x in List[1:] if x>=List[0]])
return List

while processing, the list changes, but those changes remain inside
the function, so that once it''s over, if nothis catches the return,
the original List remains unchanged.

If not a solution, I would like to at least know why it does what it
does. I so understand that List(above) is only a reference to the
actual data(a list), but I''m not passing a copy of the data to the
function, but the actual reference(hence, insertSort does
modifications). But then how can I change, inside the function, the
object List is referencing to? If I can''t modify the original list,
maybe I can make the variable List point to another list? But changes
inside the function are local. Sorry if this is a bit confusing.



它做了它的功能,因为在你的返回语句中,你需要
连接qsort(... x< ..)+ List [ 0:1] + qsort(... x> = ..)你创建一个新的

列表。在插入排序中,您可以通过List [j] = List [j-1]或List [j] = value直接修改列表的值




如果您只需要修改列表,请创建另一个调用qsort的

包装函数,然后将结果中的所有数据

复制到原始列表,你完成了。东西

喜欢:

def qsort_in_place(L):

sortedL = qsort(L)

for(i ,x)枚举(sortedL):

L [i] = x


干杯,

-Nick Vatamaniuc

It does what it does because in the return statement when you
concatenate qsort(...x<..)+List[0:1]+qsort(...x>=..) you create a new
list. In the insertion sort you modify the values of the list directly
by doing List[j]=List[j-1] or List[j]=value.

If you just have to have the list modified in place, create another
wrapper function that calls your qsort and then will copy all data
from the result into the original list and you are done. Something
like:
def qsort_in_place(L):
sortedL=qsort(L)
for (i,x) in enumerate(sortedL):
L[i]=x

Cheers,
-Nick Vatamaniuc


我明白了。我认为列表推导得到了另一个列表(duh),但

我以为我可以将对象(List)重新链接到新列表并保留它

一旦函数结束。


是否可以将一个引用(一个对象......比如''List'',

基本上)传递给一个函数并更改引用指向

在函数内创建的东西?或者从

函数中遗漏的所有数据都丢失了并且没有引用?(第二,我猜,因为

它是本地临时范围,但是你永远不知道,也许Python可以:))

I see. I figured that list comprehensions made another list(duh), but
I thought I could relink the object(List) to the new list and keep it
once the function ended.

Is it possible to pass a reference(to an object.. Like ''List'',
basically) to a function and change the reference to point to
something created inside a function? Or all data unreturned from a
function is lost and no references kept?(The second, I''d guess, since
it''s local temporary scope, but you never know, maybe Python can :) )


这篇关于排序麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆