473,548 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting by item_in_another _list

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.
Oct 24 '06 #1
17 1100
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_b y_in_list(B))
A
[2, 3, 7, 8, 0, 1, 4, 5, 6, 9, 10]
Hope it helps someone,

Cameron.
Oct 24 '06 #2
"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]
Oct 24 '06 #3
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_resu lt = [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.
Oct 24 '06 #4
Cameron Walsh <ca***********@ gmail.comwrites :
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]
How about:

desired_result = B + sorted(x for x in A if x not in B)
Oct 24 '06 #5
ZeD
Paul Rubin wrote:
>A = [0,1,2,3,4,5,6,7 ,8,9,10]
B = [2,3,7,8]

desired_resu lt = [2,3,7,8,0,1,4,5 ,6,9,10]

How about:

desired_result = B + sorted(x for x in A if x not in B)
this. is. cool.

--
Under construction
Oct 25 '06 #6
ZeD <vi***********@ gmail.comwrites :
desired_result = B + sorted(x for x in A if x not in B)
this. is. cool.
Actually it's better to use a set if B is large:

B2 = set(B)
desired_result = B + sorted(x for x in A if x not in B2)

That avoids a linear search through B for each element of A.
Oct 25 '06 #7
Paul Rubin wrote:
>for example:

A = [0,1,2,3,4,5,6,7 ,8,9,10]
B = [2,3,7,8]

desired_resu lt = [2,3,7,8,0,1,4,5 ,6,9,10]

How about:

desired_result = B + sorted(x for x in A if x not in B)
assuming that "keep the existing order" means what it says, you might as
well replace "sorted" with a list comprehension.

</F>

Oct 25 '06 #8
Fredrik Lundh <fr*****@python ware.comwrites:
desired_result = B + sorted(x for x in A if x not in B)
assuming that "keep the existing order" means what it says, you might
as well replace "sorted" with a list comprehension.
Hmm. I didn't read it that way, but yeah, if that's what the OP meant
then the sort could be skipped. I thought he just wanted a stable
sort, which the sorting primitives now promise.
Oct 25 '06 #9
ZeD wrote:
Paul Rubin wrote:
>>A = [0,1,2,3,4,5,6,7 ,8,9,10]
B = [2,3,7,8]

desired_resul t = [2,3,7,8,0,1,4,5 ,6,9,10]
How about:

desired_result = B + sorted(x for x in A if x not in B)

this. is. cool.
Cool, yes, but I'm not entirely sure it does what the OP wanted. Partly
because I'm not entirely sure what the OP wanted. Counter example:

Given these variables:

A = [0,1,2,3,4,5,6,8 ,9,10] # Note 7 is missing
B = [2,3,7,8]

which of the following should the function yield?

desired_result = [2,3,7,8,0,1,4,5 ,6,9,10]
desired_result2 = [2,3,8,0,1,4,5,6 ,9,10]

The fact that we are ostensibly sorting A makes me thing it should be
the latter, but the example given was ambiguous. If we are in fact
looking for desired_result2 , maybe we should use:

result = [ x for x in B if x in A ] + [ x for x in A if X not in B ]

or like the sibling post suggests: substitute set(A) and set(B) for the
"in" argument in each comprehension.

Cheers,
Cliff
Oct 25 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2521
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
7
3242
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
19
25434
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to sort columns based on the column header the user has clicked in both Ascending and Descending formats.
10
2752
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to write such layers myself I got stuck on how to get filtered or sorted data from the data-layer. This is what I got: Objects
4
3088
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in grid. And with rebinding, sorting image (little triangle on the column header) goes away. I need to show sorting image as well custom sorting. Please...
7
4804
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want...
1
7170
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find...
5
3166
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user...
5
4911
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link:...
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.