471,873 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,873 software developers and data experts.

Sorting a list on every nth value

Hey all,

I am very new to python, and not usually a programmer. Have mercy, please :) I am comparing road address ranges to find inconsistencies. Some roads are much longer than others and are represented by many database records. Sucking the data into python, I have arranged it like this:

{CASTLERIDGE: [3281, 1000, 1099, 21553, 900, 999]}
{BROOKRIDGE: [3265, 1000, 1099, 3276, 1100, 1199, 16398, 1200, 1299]}

where the dictionary key is the road name, the value is a repeating sequence consisting of the database key, the low address range, and the high address range. CASTLERIDGE is out of sequence, and needs to be sorted by the second value (low address range) of each sequence. It should look like this:

{CASTLERIDGE: [21553, 900, 999, 3281, 1000, 1099]}

After I get the data sorted, I then plan to check if element[4] < element[2].
If true, then return element[0] and element[3] to a list of naughty members. Then, step to the next sequence and run the comparison again.

I would like suggestions on sorting the data by the second element of each sequence, but I am also open to restructuring my data to improve sort performance and/or comparison.

Many thanks,
Matt
Nov 17 '06 #1
5 2432
kudos
127 Expert 100+
I didn't really understand how you wanted to sort the values? Care to explain it?

-kudos


Hey all,

I am very new to python, and not usually a programmer. Have mercy, please :) I am comparing road address ranges to find inconsistencies. Some roads are much longer than others and are represented by many database records. Sucking the data into python, I have arranged it like this:

{CASTLERIDGE: [3281, 1000, 1099, 21553, 900, 999]}
{BROOKRIDGE: [3265, 1000, 1099, 3276, 1100, 1199, 16398, 1200, 1299]}

where the dictionary key is the road name, the value is a repeating sequence consisting of the database key, the low address range, and the high address range. CASTLERIDGE is out of sequence, and needs to be sorted by the second value (low address range) of each sequence. It should look like this:

{CASTLERIDGE: [21553, 900, 999, 3281, 1000, 1099]}

After I get the data sorted, I then plan to check if element[4] < element[2].
If true, then return element[0] and element[3] to a list of naughty members. Then, step to the next sequence and run the comparison again.

I would like suggestions on sorting the data by the second element of each sequence, but I am also open to restructuring my data to improve sort performance and/or comparison.

Many thanks,
Matt
Nov 17 '06 #2
Alrighty, my data coming in is a table with four fields, OBJECTID, STREETNAME, LOW, and HIGH. Streets are broken up into blocks, so that main street will have a record for the 100 block, one for the 200 block and so on. The incoming table will look like this

OBJECTID | STREETNAME | LOW | HIGH
123456 | MAIN ST | 200 | 299
123457 | MAIN ST | 100 | 199
123458 | MAIN ST | 300 | 399

I felt it appropriate to load the data into a dictionary of lists, each list varying in size according to the number of records in each street. Worse, the list is a repeating sequence consisting of the database objectID, the LOW value of the block and the HIGH value of the block. When I sort the list, these three values need to move as one. The list for three blocks of MAIN ST above looks like this:

{'MAIN ST': [123456, 200, 299, 123457, 100, 199, 123458, 300, 399]}

Sorting on the LOW value, items[3,4,5] need to be popped from the list and inserted at the beginning, keeping this whole sequence together.

{'MAIN ST': [123457, 100, 199, 123456, 200, 299, 123458, 300, 399]}

Thanks again,
Matt
Nov 17 '06 #3
kudos
127 Expert 100+
I took the liberty of modifying your datastucture (I know its kind of cheating) but you can modify it back later...

Expand|Select|Wrap|Line Numbers
  1. # modifying the datastructure..
  2.  
  3. a = [123456, 200, 299, 123457, 100, 199, 123458, 300, 399]
  4. b = []
  5.  
  6. j = 0
  7. for i in range(len(a)/3):
  8.  b.append(a[j:j+3])
  9.  j+=3
  10.  
  11. # sorting it..
  12. def compare(x,y):
  13.  if(x[1]>y[1]):
  14.   return 1
  15.  elif(x[1]<y[1]):
  16.   return -1
  17.  return 0
  18.  
  19. b.sort(compare)
  20.  
  21. print b
  22.  
-kudos

Alrighty, my data coming in is a table with four fields, OBJECTID, STREETNAME, LOW, and HIGH. Streets are broken up into blocks, so that main street will have a record for the 100 block, one for the 200 block and so on. The incoming table will look like this

OBJECTID | STREETNAME | LOW | HIGH
123456 | MAIN ST | 200 | 299
123457 | MAIN ST | 100 | 199
123458 | MAIN ST | 300 | 399

I felt it appropriate to load the data into a dictionary of lists, each list varying in size according to the number of records in each street. Worse, the list is a repeating sequence consisting of the database objectID, the LOW value of the block and the HIGH value of the block. When I sort the list, these three values need to move as one. The list for three blocks of MAIN ST above looks like this:

{'MAIN ST': [123456, 200, 299, 123457, 100, 199, 123458, 300, 399]}

Sorting on the LOW value, items[3,4,5] need to be popped from the list and inserted at the beginning, keeping this whole sequence together.

{'MAIN ST': [123457, 100, 199, 123456, 200, 299, 123458, 300, 399]}

Thanks again,
Matt
Nov 17 '06 #4
bartonc
6,596 Expert 4TB
Alrighty, my data coming in is a table with four fields, OBJECTID, STREETNAME, LOW, and HIGH. Streets are broken up into blocks, so that main street will have a record for the 100 block, one for the 200 block and so on. The incoming table will look like this

OBJECTID | STREETNAME | LOW | HIGH
123456 | MAIN ST | 200 | 299
123457 | MAIN ST | 100 | 199
123458 | MAIN ST | 300 | 399

I felt it appropriate to load the data into a dictionary of lists, each list varying in size according to the number of records in each street. Worse, the list is a repeating sequence consisting of the database objectID, the LOW value of the block and the HIGH value of the block. When I sort the list, these three values need to move as one. The list for three blocks of MAIN ST above looks like this:

{'MAIN ST': [123456, 200, 299, 123457, 100, 199, 123458, 300, 399]}

Sorting on the LOW value, items[3,4,5] need to be popped from the list and inserted at the beginning, keeping this whole sequence together.

{'MAIN ST': [123457, 100, 199, 123456, 200, 299, 123458, 300, 399]}

Thanks again,
Matt
So, if I'm reading this correctly, you've got a SQL database (sqlite?, MySql?) and maybe you are doing something like:
SELECT * FROM "streets" WHERE "STREETNAME" = "MAIN ST"

If this is correct and the data in the database is in question (you want to use python to find faulty data) then you are on the right track and we'll work out a python solution.
If the data is good and in a SQL database, we'll tweek your SELECT statement and put the objects of the cursor into a neat data structure to suit your purposes. So please show use (using code tags) the lines of code that pull the data in and the purpose of sorting outside the database (maybe it's a text file). Thanks,
Barton
Nov 17 '06 #5
So, if I'm reading this correctly, you've got a SQL database (sqlite?, MySql?) and maybe you are doing something like:
SELECT * FROM "streets" WHERE "STREETNAME" = "MAIN ST"

If this is correct and the data in the database is in question (you want to use python to find faulty data) then you are on the right track and we'll work out a python solution.
If the data is good and in a SQL database, we'll tweek your SELECT statement and put the objects of the cursor into a neat data structure to suit your purposes. So please show use (using code tags) the lines of code that pull the data in and the purpose of sorting outside the database (maybe it's a text file). Thanks,
Barton
It is in an SQL database, but it is a GIS implementation. It is cross-platform, and aggregate functions within the GIS framework are severely limited--which keeps the geographic component tied to the database component. Otherwise, I would have a more elegant SQL statement, and not this problem.

Thank you,

Thank you Kudos--I can make sense of your post, and I think I can make it work. It's much more elegant than what I have hacked out to date.
Nov 21 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

18 posts views Thread by Matthias Kaeppler | last post: by
19 posts views Thread by Owen T. Soroke | last post: by
1 post views Thread by jmdolinger | last post: by
11 posts views Thread by garyhoran | last post: by
5 posts views Thread by lemlimlee | last post: by
5 posts views Thread by jrod11 | last post: by
reply views Thread by YellowAndGreen | last post: by

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.