Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old November 26th, 2003, 06:48 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
Keeping items in a list/tuple/dictionary in a specific order

I'm sure I read about this somewhere, but I can't think where, so here goes

How do you keep items in a python object (e.g. list/tuple/dictionary) in a specific order? For example, I take some rows from a database ordered according to their date, but when I then put them into a python object this order is lost!

Any clever ideas on how to avoid this?

Reply With Quote
  #2  
Old November 26th, 2003, 08:32 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Tuples seem like the way to go, once you have data in a tuple you cant even sort! Can you give an example of output from your database? Say the list returned by fetchall (or equiv')

Quote:
I take some rows from a database ordered according to their date, but when I then put them into a python object this order is lost!


Ok you lost me there, i get that your selecting from a database in order of there dates, but are you saying that fetchall is returning them in the wrong order?

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old November 26th, 2003, 09:27 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
Sorry, I'll make this a little clearer. I'm doing this:

Code:
# Psuedo-code
data = get from db (returned as list of dictionaries sorted by date)

for item in data:
    do some work on item and put it into a new dictionary

# later in code
for item in dictionary:
    process and print


The obvious problem here is that I'm not retaining the order created when I fetch the data from the db. I'd like to be able to get around that without needing to change the two sets of processing loops. I just can't think how

Reply With Quote
  #4  
Old November 26th, 2003, 03:16 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
So your returning a list of dictionaries i.e.

l = [{'a': 'b', 'c': 'd'}, {'e': 'f', 'g': 'h'}]

Mmmm, in this case the list should keep the dictionaries in the order they where added to it..

Do you have your code for this? Obviously something must be happeing between the DB rerieving the results with fetchall() and you adding it to your dictionary and adding it to the list..

Mark.

Reply With Quote
  #5  
Old November 26th, 2003, 05:27 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
Here's the code:

Code:
# Main bit of code:
raw_days = mydb.fetchRows('s_dates', 'hitdate,hits', order='hitdate,desc')
	list_dates = {}
	for item in raw_days:
		# Do the processing

# From db.py, the part that creates the list of dicts:
fulldata = []
for x in range(0,numrows):
	row = self.cursor.fetchone()
	data = {}
	i = 0
	for key in string.split(columns, ','):
		data[key] = row[i]
		i = i + 1
	fulldata.append(data)


So, in other words, an SQL query is sent, asking for a list of rows ordered by a date column. The part from db.py shown iterates through this list fetching each row in turn and appending it as a nested dictionary to a new list, which is returned. This new list is now iterated over, processing each item.

The conundrum is that no matter how I sort the SQL query, the processed data always comes out in the same order, and it's not the order I want!

Reply With Quote
  #6  
Old November 26th, 2003, 08:22 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
The main point of dictionaries being diff to lists/tuples is that you can access them in constant time no matter how many items there are stored. The downside is you can't maintain them in sorted order. For that you need a list/tuple.

tho you could probably print them out in printed if you're iterating over them. I'm guessing that you (or the predefined function if it exists) turns it into a list first and sorts it first .. so would be alotof work each time you printed it out if its alot of data.

you need to work out what is more important if using large data - speed or retrival of single item, or printing it out sorted

Reply With Quote
  #7  
Old November 27th, 2003, 02:34 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
so your fetching one set of results at a time, which i'm assuming means that row looks like this..

row = [1, 2, 3, 4]

your then adding each of these to a dictionary with its colum name asthe key. stop me if i'm wrong. this dictionary, now looking like this..

dict = {'col1': 1, 'col2: 2, 'col3': 3, 'col4': 4}

is then appened to a list, leaving something like the example in my earlier post? This is then returned.. first it seems like fetchall() would be a better choice for preformance and may also give you better results with the keepin in order thing?

I try will give it a go now i see what you mean and let you know

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Keeping items in a list/tuple/dictionary in a specific order


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway