按item_in_another_list排序 [英] Sorting by item_in_another_list
问题描述
我有两个名单,A和B,这样B就是A的子集。
我希望排序A使得B中的元素位于A的开头,
并保持现有的顺序,即稳定排序。 B中
元素的顺序总是正确的。
例如:
A = [0 ,1,2,3,4,5,6,7,8,9,10]
B = [2,3,7,8]
desired_result = [2,3,7,8,0,1,4,5,6,9,10]
目前我已经定义了比较器功能:
def sort_by_in_list(x,y):
ret = 0
如果B中的x:
ret - = 1
如果你在B:
ret + = 1
返回返回
和am使用:
A.sort(sort_by_in_list)
确实产生了预期的结果。
我现在有几个问题:
1.)这对于多达500个元素来说是最有效的方法吗?
如果没有,那会是什么?更好吗?
2.)这个当前版本不允许我为B选择一个不同的列表
。是否有某些描述的bind_third函数我可以
用于定义具有3个参数的新函数,将其提供给t他排名第三(
列表排序),并让A.sort(sort_by_in_list)提供其他
2变量?
问候对所有人来说,
Cameron。
Cameron Walsh写道:
我有两个列表,A和B,这样B就是A的子集。
我希望对A进行排序,使B中的元素位于A的开头,
并保持现有的顺序,即稳定排序。 B中
元素的顺序总是正确的。
例如:
A = [0 ,1,2,3,4,5,6,7,8,9,10]
B = [2,3,7,8]
desired_result = [2,3,7,8,0,1,4,5,6,9,10]
目前我已经定义了比较器功能:
def sort_by_in_list(x,y):
ret = 0
如果在B中x:
ret - = 1
如果在B中是y:
ret + = 1
return ret
和我正在使用:
A.sort(sort_by_in_list)
确实产生了预期的效果。
>
我现在有几个问题:
1.)这是最多500个元素的最有效方法吗?如果
没有,那会更好吗?
2.)这个当前版本不允许我为B选择不同的列表
是否有某些描述的bind_third函数,我可以使用
来定义一个带有3个参数的新函数,将它提供给第三个(
列表进行排序),以及有A.sort(sort_by_in_list)提供其他
2个变量吗?
问候所有人,
卡梅伦。
我找到第二个问题的答案如下:
< blockquote class =post_quotes>
>> A = [0,1,2,3,4,5,6,7,8,9,10]
B = [2,3 ,7,8]
def sort_by_in_list(in_list):
def ret_function(x,y):
ret = 0
如果在in_list中为x:
ret - = 1
如果在in_list中为y:
ret + = 1
返回ret
返回ret_function
>> A.sort(sort_by_in_list(B))
A
[2,3,7,8,0,1,4,5,6,9,10]
希望对某人有所帮助,
Cameron。
" Cameron Walsh" < ca *********** @ gmail.comwrote in message
news:eh ********** @ enyo.uwa.edu.au。 ..
我有两个列表,A和B,这样B就是A的子集。
我希望对A进行排序,使得B中的元素位于A的开头,
并保持现有的顺序,即稳定排序。 B中
元素的顺序总是正确的。
例如:
A = [0 ,1,2,3,4,5,6,7,8,9,10]
B = [2,3,7,8]
desired_result = [2,3,7,8,0,1,4,5,6,9,10]
目前我已经定义了比较器功能:
def sort_by_in_list(x,y):
ret = 0
如果在B中x:
ret - = 1
如果在B中是y:
ret + = 1
return ret
和我正在使用:
A.sort(sort_by_in_list)
确实产生了预期的效果。
>
我现在有几个问题:
1.)这是最多500个元素的最有效方法吗?如果
没有,那会更好吗?
2.)这个当前版本不允许我为
B选择不同的列表。是否有一些bind_third函数的某些描述我可以使用
定义一个带有3个参数的新函数,将它提供给第三个(列表为
排序依据),以及让A.sort(sort_by_in_list)提供其他2
变量?
用Python思考。定义一个函数来获取列表,并具有该函数
返回正确的比较函数。这让我有机会将输入列表转换为一组,这将有助于将我的列表扩展到数百个元素。见下文。
- 保罗
def sort_by_in_list(reflist):
reflist = set(reflist)
def sort_by_in_list_(x,y):
ret = 0
如果x在reflist中:ret - = 1
如果y在reflist中:ret + = 1
返回ret
返回sort_by_in_list_
A = [0,1,2,3, 4,5,6,7,8,9,10]
B = [2,3,7,8]
A.sort(sort_by_in_list(B))
打印A
给:
[2,3,7,8,0,1,4,5,6 ,9,10]
Paul McGuire写道:
" Cameron Walsh" < ca *********** @ gmail.comwrote in message
news:eh ********** @ enyo.uwa.edu.au。 ..
>
我有两个列表,A和B,这样B就是A的一个子集。 />
我希望对A进行排序,使得B中的元素位于A的开头,
并保持现有的顺序,即稳定排序。 B中
元素的顺序总是正确的。
例如:
A = [0,1,2,3,4,5,6 ,7,8,9,10]
B = [2,3,7,8]
desired_result = [2,3,7,8,0,1,4,5 ,6,9,10]
目前我已经定义了比较器功能:
def sort_by_in_list(x,y):
ret = 0
如果在B中的x:
ret - = 1
如果在B中的y:
ret + = 1
返回ret
并且正在使用:
A.sort(sort_by_in_list)
确实产生了预期的效果。
我现在有几个问题:
1。)这是最多500个元素的最有效方法吗?如果不是,那会更好?
2.)这个当前版本不允许我为
B选择不同的列表。是否有一些bind_third函数的某些描述,我可以使用它来定义一个带有3个参数的新函数,将它提供给第三个(列表按
排序),并使用A.sort(sort_by_in_list)提供其他2个变量?
用Python思考。定义一个函数来获取列表,并具有该函数
返回正确的比较函数。这让我有机会将输入列表转换为一组,这将有助于将我的列表扩展到数百个元素。见下文。
- Paul
def sort_by_in_list(reflist):
reflist = set(reflist)
def sort_by_in_list_(x,y):
ret = 0
如果x在reflist中:ret - = 1
如果你在reflist中:ret + = 1
return ret
return sort_by_in_list_
A = [0,1, 2,3,4,5,6,7,8,9,10]
B = [2,3,7,8]
A.sort(sort_by_in_list (B))
打印A
给:
[2,3,7,8,0,1,4 ,5,6,9,10]
看起来我们的答案是交叉的。必须在Python中学习集合...
非常感谢,
Cameron。
>
Hi,
I have two lists, A and B, such that B is a subset of A.
I wish to sort A such that the elements in B are at the beginning of A,
and keep the existing order otherwise, i.e. stable sort. The order of
elements in B will always be correct.
for example:
A = [0,1,2,3,4,5,6,7,8,9,10]
B = [2,3,7,8]
desired_result = [2,3,7,8,0,1,4,5,6,9,10]
At the moment I have defined a comparator function:
def sort_by_in_list(x,y):
ret = 0
if x in B:
ret -= 1
if y in B:
ret += 1
return ret
and am using:
A.sort(sort_by_in_list)
which does produce the desired results.
I do now have a few questions:
1.) Is this the most efficient method for up to around 500 elements?
If not, what would be better?
2.) This current version does not allow me to choose a different list
for B. Is there a bind_third function of some description that I could
use to define a new function with 3 parameters, feed it the third (the
list to sort by), and have the A.sort(sort_by_in_list) provide the other
2 variables?
Regards to all,
Cameron.
Cameron Walsh wrote:Hi,
I have two lists, A and B, such that B is a subset of A.
I wish to sort A such that the elements in B are at the beginning of A,
and keep the existing order otherwise, i.e. stable sort. The order of
elements in B will always be correct.
for example:
A = [0,1,2,3,4,5,6,7,8,9,10]
B = [2,3,7,8]
desired_result = [2,3,7,8,0,1,4,5,6,9,10]
At the moment I have defined a comparator function:
def sort_by_in_list(x,y):
ret = 0
if x in B:
ret -= 1
if y in B:
ret += 1
return ret
and am using:
A.sort(sort_by_in_list)
which does produce the desired results.
I do now have a few questions:
1.) Is this the most efficient method for up to around 500 elements? If
not, what would be better?
2.) This current version does not allow me to choose a different list
for B. Is there a bind_third function of some description that I could
use to define a new function with 3 parameters, feed it the third (the
list to sort by), and have the A.sort(sort_by_in_list) provide the other
2 variables?
Regards to all,
Cameron.
Well I found an answer to the second question with the following:
>>A=[0,1,2,3,4,5,6,7,8,9,10]
B=[2,3,7,8]
def sort_by_in_list(in_list):
def ret_function(x,y):
ret = 0
if x in in_list:
ret -= 1
if y in in_list:
ret += 1
return ret
return ret_function
>>A.sort(sort_by_in_list(B))
A
[2, 3, 7, 8, 0, 1, 4, 5, 6, 9, 10]
Hope it helps someone,
Cameron.
"Cameron Walsh" <ca***********@gmail.comwrote in message
news:eh**********@enyo.uwa.edu.au...Hi,
I have two lists, A and B, such that B is a subset of A.
I wish to sort A such that the elements in B are at the beginning of A,
and keep the existing order otherwise, i.e. stable sort. The order of
elements in B will always be correct.
for example:
A = [0,1,2,3,4,5,6,7,8,9,10]
B = [2,3,7,8]
desired_result = [2,3,7,8,0,1,4,5,6,9,10]
At the moment I have defined a comparator function:
def sort_by_in_list(x,y):
ret = 0
if x in B:
ret -= 1
if y in B:
ret += 1
return ret
and am using:
A.sort(sort_by_in_list)
which does produce the desired results.
I do now have a few questions:
1.) Is this the most efficient method for up to around 500 elements? If
not, what would be better?
2.) This current version does not allow me to choose a different list for
B. Is there a bind_third function of some description that I could use to
define a new function with 3 parameters, feed it the third (the list to
sort by), and have the A.sort(sort_by_in_list) provide the other 2
variables?
Think in Python. Define a function to take the list, and have that function
return the proper comparison function. This gives me the chance to also
convert the input list to a set, which will help in scaling up my list to
hundreds of elements. See below.
-- Paul
def sort_by_in_list(reflist):
reflist = set(reflist)
def sort_by_in_list_(x,y):
ret = 0
if x in reflist: ret -= 1
if y in reflist: ret += 1
return ret
return sort_by_in_list_
A = [0,1,2,3,4,5,6,7,8,9,10]
B = [2,3,7,8]
A.sort( sort_by_in_list(B) )
print A
Gives:
[2, 3, 7, 8, 0, 1, 4, 5, 6, 9, 10]
Paul McGuire wrote:"Cameron Walsh" <ca***********@gmail.comwrote in message
news:eh**********@enyo.uwa.edu.au...>Hi,
I have two lists, A and B, such that B is a subset of A.
I wish to sort A such that the elements in B are at the beginning of A,
and keep the existing order otherwise, i.e. stable sort. The order of
elements in B will always be correct.
for example:
A = [0,1,2,3,4,5,6,7,8,9,10]
B = [2,3,7,8]
desired_result = [2,3,7,8,0,1,4,5,6,9,10]
At the moment I have defined a comparator function:
def sort_by_in_list(x,y):
ret = 0
if x in B:
ret -= 1
if y in B:
ret += 1
return ret
and am using:
A.sort(sort_by_in_list)
which does produce the desired results.
I do now have a few questions:
1.) Is this the most efficient method for up to around 500 elements? If
not, what would be better?
2.) This current version does not allow me to choose a different list for
B. Is there a bind_third function of some description that I could use to
define a new function with 3 parameters, feed it the third (the list to
sort by), and have the A.sort(sort_by_in_list) provide the other 2
variables?
Think in Python. Define a function to take the list, and have that function
return the proper comparison function. This gives me the chance to also
convert the input list to a set, which will help in scaling up my list to
hundreds of elements. See below.
-- Paul
def sort_by_in_list(reflist):
reflist = set(reflist)
def sort_by_in_list_(x,y):
ret = 0
if x in reflist: ret -= 1
if y in reflist: ret += 1
return ret
return sort_by_in_list_
A = [0,1,2,3,4,5,6,7,8,9,10]
B = [2,3,7,8]
A.sort( sort_by_in_list(B) )
print A
Gives:
[2, 3, 7, 8, 0, 1, 4, 5, 6, 9, 10]
Looks like our answers crossed-over. Must learn about sets in Python...
Thanks very much,
Cameron.
这篇关于按item_in_another_list排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!