determining the bounds of a tuple returned from a database

John Machin sjmachin at lexicon.net
Fri Nov 17 03:24:15 EST 2006


ronrsr top-posted [uncorrected]:
> very sorry, that was my error - len(result[0]) and len(result[1]) both
> return 1 --
>
> i think I'm misunderstanding what len() does - to me they appear to
> have 2 or 3 elements, or at least be composed of a string of some
> length.

len(result) is 248 because you have retrieved 248 rows from the
database.

len(result[0]), len(result[1]), etc etc are all each 1 because you have
selected only one column from each row -- I'm guessing here because you
have not supplied the SQL query [that would have been much better than
the last 200 rows of result!]

To get the one column out, you need to select the first (and only!)
column from each row.

result[0][0]
result[1][0]
etc

>
> I guess len() isn't the function i'm looking for then.  How do I tell
> how many strings, or how many bytes are in each dimension?  so that I
> can iterate through them.

len() *is* the function that you are looking for. The problem that
needs to be solved before we can help you is that we don't understand
your data. Stripped of all the Python "punctuation", what you would see
if you used some user-interface tool [I'm not familiar with mysql] to
"print" each row to your screen  *might* be something like this [it
would number lines from 1 but we'll do it from 0 to avoid doubling the
confusion]

0: Agricultural subsidies; Foreign aid
1: Agriculture; Sustainable Agriculture - Support; Organic Agriculture;
Pesticides, US, Childhood Development, Birth Defects; Toxic Chemicals
2: Antibiotics, Animals
3: Agricultural Subsidies, Global Trade
4: Agricultural Subsidies
5: Biodiversity
etc

Looks like each line contains one or more topics separated by
semicolons. E.g. in line 0,
"Agricultural subsidies" and "Foreign aid" are 2 topics. Line 1
contains 5 topics.

Note carefully:
[1] Line 4 has 1 topic; it is essentially the *same* topic as the first
topic in line 0 but note "S" versus "s"
[2] Either Line 3 has 1 topic which is a *subtopic* of "Agricultural
Subsidies" *OR* the comma should be a semi-colon [so that it would have
2 topics, very similar to line 0] -- in any case, this is a
complication ....

>
> Each entry in that list is a keyword

What do you mean by "entry" and "list" and "keyword"? Give examples.

 - Alternately, is there any fast
> way to parse that into a sorted list of distinct keywords.

Quite possibly, if only you could say what you mean by "keywords".
Please give a *small* example, like what you would expect from the
first 6 lines as quoted above.

Multi-topic lines can be split up by using line.split(";") -- but
understanding (at least partial)  should come before coding, IMHO.

HTH,
John

> Fredrik Lundh wrote:
> > ronrsr wrote:
> >
> > > it looks like the  len() function is the one I want.
> > >
> > > for:   len(result) - i get 248,
> > >
> > > but for len(result[0]) or len(result[1]) i always get 0.
> >
> > that's a bit surprising, because both items are tuples that contain
> > exactly one item:
> >




More information about the Python-list mailing list