473,624 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select as dictionary...

Hi..
I am using python with postgresql.
And i have a query :

aia.execute("SE LECT id, w from list")
links=aia.fetch all()
print links

and result
[(1, 5), (2,5).......] (2 million result)

I want to see this result directly as a dictionary:

{1: 5, 2: 5 .....}

How do i select in this format ?

I'm sorry my bad english.

King Regards...

Oct 1 '07 #1
17 2414
Besturk.Net Admin a écrit :
Hi..
I am using python with postgresql.
And i have a query :

aia.execute("SE LECT id, w from list")
links=aia.fetch all()
print links

and result
[(1, 5), (2,5).......] (2 million result)

I want to see this result directly as a dictionary:

{1: 5, 2: 5 .....}

How do i select in this format ?
IIRC, postgres' db-api connector (well, at least one of them - I don't
know which one you're using) has a DictCursor. You should find all you
want to know in the relevant doc.

FWIW:
http://www.initd.org/tracker/psycopg/wiki/PsycopgTwoFaq

HTH
Oct 1 '07 #2
On Mon, Oct 01, 2007 at 06:32:07AM -0700, Besturk.Net Admin wrote regarding Select as dictionary...:
>
aia.execute("SE LECT id, w from list")
links=aia.fetch all()
print links

and result
[(1, 5), (2,5).......] (2 million result)

I want to see this result directly as a dictionary:

{1: 5, 2: 5 .....}

How do i select in this format ?
Try this:

aia.execute("SE LECT id, w from list")
links=aia.fetch all()
linkdict = dict()
for k,v in links:
linkdict[k] = v
print linkdict
Cheers,
Cliff
Oct 1 '07 #3
On Mon, 01 Oct 2007 09:57:46 -0400, J. Clifford Dyer wrote:
On Mon, Oct 01, 2007 at 06:32:07AM -0700, Besturk.Net Admin wrote
regarding Select as dictionary...:
>>
aia.execute("S ELECT id, w from list") links=aia.fetch all()
print links

and result
[(1, 5), (2,5).......] (2 million result)

I want to see this result directly as a dictionary:

{1: 5, 2: 5 .....}

How do i select in this format ?
Try this:

aia.execute("SE LECT id, w from list")
links=aia.fetch all()
linkdict = dict()
for k,v in links:
linkdict[k] = v
print linkdict
Besides using the already pointed out DB adapters, you can easily
transform a list of items into a dictionary with the `dict` constructor::
>>links = [(1, 5), (2, 5), (3, 10)]
dict(links)
{1: 5, 2: 5, 3: 10}

Stargaming
Oct 1 '07 #4
On 2007-10-01 15:32, Besturk.Net Admin wrote:
Hi..
I am using python with postgresql.
And i have a query :

aia.execute("SE LECT id, w from list")
links=aia.fetch all()
print links

and result
[(1, 5), (2,5).......] (2 million result)

I want to see this result directly as a dictionary:

{1: 5, 2: 5 .....}

How do i select in this format ?
You can easily convert the above list to a dictionary:

d = dict(links)

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Oct 01 2007)
>>Python/Zope Consulting and Support ... http://www.egenix.com/
mxODBC.Zope.D atabase.Adapter ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
_______________ _______________ _______________ _______________ ____________

:::: Try mxODBC.Zope.DA for Windows,Linux,S olaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
Oct 1 '07 #5
"J. Clifford Dyer" <jc*@sdf.lonest ar.orgwrote:
aia.execute("SE LECT id, w from list")
links=aia.fetch all()
linkdict = dict()
for k,v in links:
linkdict[k] = v
print linkdict
Wouldn't it be simpler just to do:

aia.execute("SE LECT id, w from list")
linkdict=dict(a ia.fetchall())

even better would be to use an iterator to avoid fetching the entire
resultset as a list.
Oct 1 '07 #6
On Mon, 2007-10-01 at 09:57 -0400, J. Clifford Dyer wrote:
On Mon, Oct 01, 2007 at 06:32:07AM -0700, Besturk.Net Admin wrote regarding Select as dictionary...:

aia.execute("SE LECT id, w from list")
links=aia.fetch all()
print links

and result
[(1, 5), (2,5).......] (2 million result)

I want to see this result directly as a dictionary:

{1: 5, 2: 5 .....}

How do i select in this format ?
Try this:

aia.execute("SE LECT id, w from list")
links=aia.fetch all()
linkdict = dict()
for k,v in links:
linkdict[k] = v
print linkdict
Improvement 1: Use the fact that dict can be initialized from a sequence
of key/value pairs:

aia.execute("SE LECT id, w from list")
linkdict = dict(aia.fetcha ll())

Improvement 2: Use an iterator instead of reading all rows into memory:

aia.execute("SE LECT id, w from list")
linkdict = dict(iter(aia.f etchone,None))

--
Carsten Haese
http://informixdb.sourceforge.net
Oct 1 '07 #7
On Mon, 2007-10-01 at 15:50 +0200, Bruno Desthuilliers wrote:
Besturk.Net Admin a écrit :
I want to see this result directly as a dictionary:

{1: 5, 2: 5 .....}

How do i select in this format ?

IIRC, postgres' db-api connector (well, at least one of them - I don't
know which one you're using) has a DictCursor.
That would return the results as a list of dictionaries like this:

[{'id':1, 'w':5}, {'id':2, 'w':5}, ...]

--
Carsten Haese
http://informixdb.sourceforge.net
Oct 1 '07 #8
linkdict = dict(iter(aia.f etchone,None))

And by the way, that line can be shortened to "linkdict=dict( aia)" if
the cursor object supports the iterator protocol, but that's not a
mandatory feature in DB-API v2. The longer form is guaranteed to work
with any DB-API v2 compliant implementation.

--
Carsten Haese
http://informixdb.sourceforge.net
Oct 1 '07 #9
On Mon, Oct 01, 2007 at 10:12:04AM -0400, Carsten Haese wrote regarding Re: Select as dictionary...:
>
On Mon, 2007-10-01 at 09:57 -0400, J. Clifford Dyer wrote:
>
Try this:

aia.execute("SE LECT id, w from list")
links=aia.fetch all()
linkdict = dict()
for k,v in links:
linkdict[k] = v
print linkdict

Improvement 1: Use the fact that dict can be initialized from a sequence
of key/value pairs:

aia.execute("SE LECT id, w from list")
linkdict = dict(aia.fetcha ll())
This is only an improvement if the SQL query remains a list of 2-tuples. If the OP wants to add more values, the more verbose version allows for easier extensibility.

for k,v1,v2 in links:
linkdict[k] = (v1, v2)

Of course even better would be not to have to add variables, so maybe:

for link in links:
linkdict[link[0]] = link[1:],

which changes the output format somewhat, but keeps the access by dict keyed to the DB id.
Improvement 2: Use an iterator instead of reading all rows into memory:

aia.execute("SE LECT id, w from list")
linkdict = dict(iter(aia.f etchone,None))
Agreed. A much better solution.

Cheers,
Cliff
Oct 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3582
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148 respectively In the sample code you see that I have class Item and class Dict class Dict contains a dictionary called items. The items dictionary will contain instances of Item that are keyed off of the Item name. In __main__ I create two...
9
3623
by: Kevin | last post by:
Hi, I am getting a syntax error Microsoft VBScript compilation error '800a03ea' Syntax error On the code below. The error references the "End Select" line Can anyone help me with what I am doing wrong? Thanks
1
9247
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex DataBinding accepts as a data source either an IList or an IListSource at System.Windows.Forms.ListControl.set_DataSource(Object value)
4
8017
by: toglez | last post by:
Hi all! I use PyQt4 and Python to create a program to display numbers from a file in a QTable Widget, select various columns from the table and then display the selected columns in a new table. Well, I don't seem to understand how to do this. Here is my code: main.py: ------------- -----------------------------------code start -------------------------------------------------------- from PyQt4 import QtGui, QtCore
0
8242
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8681
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7170
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.