473,396 Members | 1,942 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,396 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 2621
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.