473,326 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 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 2613
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

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

Similar topics

18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
19
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...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
1
by: jmdolinger | last post by:
Hi all, I'm a newbie to Atlas (and recently ASP.NET) after coming from a long Java background, also have done quite a bit with an Ajax.NET/ASP.NET 1.1 project, but it was basically all...
11
by: garyhoran | last post by:
Hi Guys, I have a collection that contains various attributes (stuff like strings, DateTime and Timespan) . I would like to access the collection in various orders at different points in the...
12
by: Justin | last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of sorting my arraylist. I have a structure of two elements: Structure TabStructure Dim TabName As String Dim FullFilePath...
5
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...
4
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file...
5
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.