|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
||||
|
||||
|
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? |
|
#2
|
||||
|
||||
|
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:
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. |
|
#3
|
||||
|
||||
|
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 ![]() |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
||||
|
||||
|
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! |
|
#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 |
|
#7
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Keeping items in a list/tuple/dictionary in a specific order |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|