views:

83

answers:

2

hello! i read a lot in that forum, but i couldn't find a proper way to add all items to my dictionary... So maybe someone can help me! first a explanation:

rows = cur.fetchall() 
columns=[desc[0] for desc in cur.description]  
GID_Distances = {}  
if len(rows) > 0:
    for row in rows:
      items = zip(columns, row)
      GID_Distances = {}
      for (name,  value) in items:
         GID_Distances[name]=value

rows is a list from a sql-statement. so in that list are several values with the same key... I just want to get something like a table something like this: {['id': 1, 'point':2], ['id':2, 'point':3]} but for the loop above is the result just the last item, because it overwrites all before. Any ideas????

+2  A: 

you are redefining GID_Distances to a blank dictionary in the loop without first storing the value. store the rows in a list like so:

rows = cur.fetchall() 
columns=[desc[0] for desc in cur.description]  
results = []
for row in rows:
  GID_Distances = {} 
  items = zip(columns, row)
  for (name,  value) in items:
     GID_Distances[name]=value
  results.append(GID_Distances)
aaronasterling
but then this error occurs:NameError: global name 'GID_Distances' is not defined
there must be a typo then. there is no good reason for that to happen. For example, you are accessing `columns` just fine and it's defined at the same block level as `GID_Distances`. Check that your spelling is the same in both places.
aaronasterling
don't think that there is a typo, because it's the same code... i just added a #...
double check to be sure. I find a copy and paste of the variable names often works. Are there any functions involved? This sort of thing can happen because of messed up indentation.
aaronasterling
but the error occurs in the line of the loop... thats the first time i use GID_Distances, because i deleted the row before...
i will check the indentation again...
and you are defining it to be an empty dict before you enter the loops, correct?
aaronasterling
yes, thats right! before the loop, so i thought at the end it has to be filled with the three rows!
I messed up in the code but that doesn't have anything to do with the NameError bug you've been having. Try the new version and see.
aaronasterling
the new python version? can't do that because i am writing a plugin for qgis and thats just working with 2.5, or sth like that...
no. I modified the code in my post. Try that and see if it works. look at the post. you will end up with a list of dictionaries name results. each dictionary is a row. you can rename to taste but this code will do what you want.
aaronasterling
sorry, haven't seen that! mh, it's a good idea to store the hole thing in a list... the code is not working properly, but i hope it will... :)... need some time, let you know AND: Thanks a lot!
just go with kenny's solution
aaronasterling
+3  A: 

If you have an iterable of pairs i.e. [(k1,v1),(k2,v2),...], you could apply dict on it to make it a dictionary. Therefore, your code could be written simply as

rows = cur.fetchall() 
columns = [desc[0] for desc in cur.description]  
# or: columns = list(map(operator.itemgetter(0), cur.description))
#     don't call list() in Python 2.x.
GID_Distances = [dict(zip(columns, row)) for row in rows]
# note: use itertools.izip in Python 2.x
KennyTM
+1 forgot about dict
aaronasterling
That worked perfect! uhu, so much shorter... great!so, i will see if i can go on with that... have to change because i used the dictionary before!Thanks for the fast help, great!