determining the bounds of a tuple returned from a database

Discussion in 'Python' started by ronrsr, Nov 17, 2006.

  1. ronrsr

    ronrsr Guest

    here is my result.
    How do I determine the number of tuples in this array, returned from a
    mysql database?

    How do I determine the number of characters or entry in each tuple?

    thanks very much for your assistance,

    -rsr-


    (('Agricultural subsidies; Foreign aid',), ('Agriculture; Sustainable
    Agriculture - Support; Organic Agriculture; Pesticides, US, Childhood
    Development, Birth Defects; Toxic Chemicals',), ('Antibiotics,
    Animals',), ('Agricultural Subsidies, Global Trade',), ('Agricultural
    Subsidies',), ('Biodiversity',), ('Citizen Activism',), ('Community
    Gardens',), ('Cooperatives',), ('Dieting',), ('Agriculture, Cotton',),
    ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
    ('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
    Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
    Feed, Corn, Pesticides',), ('Aquaculture',), ('Chemical Warfare',),
    ('Compost',), ('Debt',), ('Consumerism',), ('Fear',), ('Pesticides, US,
    Childhood Development, Birth Defects',), ('Corporate Reform,
    Personhood (Dem. Book)',), ('Corporate Reform, Personhood, Farming
    (Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
    Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
    ('Democracy, Corporate Personhood, Porter Township (Dem. Book)',),
    ('Disaster Relief',), ('Dwellings, Slums',), ('Economics, Mexico',),
    ('Economy, Local',), ('Education, Protests',), ('Endangered Habitat,
    Rainforest',), ('Endangered Species',), ('Endangered Species,
    Extinction',), ('antibiotics, livestock',), ('Pesticides, Water',),
    ('Environment, Environmentalist',), ('Food, Hunger, Agriculture, Aid,
    World, Development',), ('Agriculture, Cotton Trade',), ('Agriculture,
    Cotton, Africa',), ('Environment, Energy',), ('Fair Trade (Dem.
    Book)',), ('Farmland, Sprawl',), ('Fast Food, Globalization,
    Mapping',), ('depression, mental illness, mood disorders',), ('Economic
    Democracy, Corporate Personhood',), ('Brazil, citizen activism, hope,
    inspiration, labor issues',), ('citizen activism, advice, hope',),
    ('Pharmaceuticals, Medicine, Drugs',), ('Community Investing',),
    ('Environment, Consumer Waste Reduction, Consumer Behavior and
    Taxes',), ('Hunger, US, Poverty',), ('FERTILITY, Women',),
    ('Corporatism, Environment',), ('Economic Democracy, Corporate
    Farming',), ('Economic Democracy, Inspiration',), ('FEAR
    (Fearlessness)',), ('Federal budget, military spending, foreign
    cultural exchange programs',), ('Fish Farming',), ('Fish population,
    commercial fishing industry',), ('GMO, BT Cotton',), ('Food borne
    Illness, Water',), ('Food Charity',), ('Food Charity, Urban Farming',),
    ('Food Consumption, Luxury',), ('Food Cost, international',), ('Food
    Disposal, Waste',), ('Food stamps, eligibility for',), ('Food,
    expenditure on',), ('Foreign Aid',), ('Genetics, Food, Agriculture,
    International',), ('Global Policy, Bush Administration',), ('Global
    Trade, food',), ('Global Trade, free trade, clothing',),
    ('Globalization',), ('GMO, SACTO Protests',), ('GMO, Wheat, trade',),
    ('Happiness and income',), ('Health Care - Spending',), ('Hope',),
    ('Hope, inspirational quote',), ('Human Cloning',), ('Hunger',),
    ('Hunger, income',), ('IMF, Argentina',), ('IMF, failings',),
    ('Inequality',), ('INEQUALITY, GLOBALIZATION, POVERTY',), ('Inequality,
    Globalization, Latin America',), ('Inspiration',), ('Junk Food',),
    ('Junk Food, Soft Drinks',), ('Junk Food ,Children, Education,
    Media',), ('Junk Food, Education, Children, Nutrition',), ('Junk Food,
    Global, Coke',), ('Junk Food, McDonald\x92s',), ('Junk Food,
    McDonald\x92s, Global',), ('Junk Food, Soft Drinks, Water',), ('Junk
    Food, Education, Nutrition, Childhood Illness',), ('Junk food,
    Nutrition, Schools',), ('Labor',), ('Labor Issues, Agriculture,
    Sustainable Agriculture Support',), ('Labor Issues, Employee
    Ownership',), ('Land Reform, Brazil, Infant mortality',),
    ('Livestock',), ('Meat',), ('Meat, Brazil, Soybeans, Deforestation',),
    ('Meat, concentration',), ('Meat, Recalls, Contamination',), ('Meat,
    Recalls',), ('Media',), ('Microloans',), ('Military Spending',),
    ('Nutrition Programs, School Meals, Snapple',), ('Nutrition,
    Finland',), ('Obesity',), ('Obesity, Childhood',), ('Obesity,
    Africa',), ('Organic Food',), ('Obesity, Global',), ('Obesity, related
    illnesses',), ('Obesity, related illnesses, children',), ('Oil',),
    ('Organic Agriculture',), ('Organic Agriculture, Growth',), ('Organic
    Agriculture, Nutrition',), ('Organic Agriculture, Productivity',),
    ('Organic food prices',), ('Community',), ('Corporate Concentration',),
    ('Community Gardens, Urban Gardens',), ('Pollution, Air, CO2
    emissions',), ('Population, Water',), ('Poverty, Aid',), ('Poverty,
    World Bank, Hunger',), ('Organic Produce',), ('Organic Produce,
    Farmer\x92s Markets, CSA Community Initiatives (Dem. Book)',),
    ('Pakistan, Education, Poverty, Terrorism',), ('Pesticides',),
    ('Pollution, US',), ('Population, Women',), ('Poverty, Labor Issues',),
    ('t2',), ('Agriculture, Subsidies, Farming, Ranching, Government,
    Livestock, Cattle',), ('School, Farmers\x92 Fresh Produce Food',),
    ('School, Fast Food, Lunch Programs',), ('School, Fast Food, Lunch
    Programs, Harms',), ('Citizens Groups',), ('Public Health, Bacteria',),
    ('Violence, Impotence',), ('Private Food Aid, Food Stamps',), ('Prison
    Rates, Race',), ('Produce, Nutritional content of produce, Historical
    Perspective',), ('Public Opinion',), ('Slavery',), ('Solar, Renewable
    Energy',), ('Suicide',), ('Sustainable agriculture, Moringa tree',),
    ('Sweatshop Labor',), ('Tax Cuts, George W. Bush',), ('Economic
    Democracy (Dem. Book)',), ('Tobacco',), ('Toxic Chemicals',),
    ('Trade',), ('Trade, Agricultural Raw materials',), ('Trade, Coffee',),
    ('Trade, Cotton',), ('Trade, NAFTA, Mexico',), ('Vegetarianism',),
    ('Veterans\x92 Benefits',), ('Violence',), ('Violence, Media,
    Children',), ('Water',), ('Water, Privatization',), ('Welfare
    Reform',), ('Wind, Renewable Energy',), ('Words',), ('World
    Population',), ('World Bank',), ('Grassroots, Community, Living Wage,
    Poverty',), ('Subsidies',), ('Protest, Action',), ('Nuclear Power
    Plants, Emissions',), ('Media, Fear, Religion, George W. Bush',),
    ('Consciousness, Change',), ('Peace, Security, Oil',), ('Globalization,
    Inequality, Commodity prices',), ('Water, Corporate',), ('Consumerism,
    Developing Nations',), ('Nuclear Power, Wind Power',),
    ('Sustainability, Hope',), ('Ozone, CFCs, Chlorine, Atmosphere',),
    ('Hydrogen, Emissions, Ozone',), ('Water, Pesticides',), ('Iraq,
    Military, Health',), ('Toxic Waste Reduction, Toxic Waste Disposal,
    Environmental Regulation, Recycling Electronics',), ('Shareholder
    Advocacy, Social Responsibility, Community Investing (Dem. Book)',),
    ('Fair Trade',), ('Genetically Modified Food, Africa, USAID, ISAAA',),
    ('Sustainability, Progress',), ('Sustainability, Human Rights,
    Globalization, Corporatization',), ('Agricultural Subsidies,
    Agricultural Policy',), ('Agricultural Subsidies, Farmers',), ('Cotton,
    Rice',), ('Agricultural Subsidies, Farmers, Livestock',),
    ('Agricultural Subsidies, Prices',), ('Food scarcity, Hunger, Urban
    Agriculture',), ('GMO farming, Toxic chemicals',), ('Hunger,
    Children',), ('Hunger, Food Insecurity',), ('Inspiration, Corporatism,
    War',), ('Inspiration, Economic Growth',), ('Land Vacancies, US Land
    Reform',), ('Local agriculture, Local produce, Nutritional content of
    local produce',), ('Local produce, Urban agriculture',), ('Pakistan,
    Religion, Education',), ('Pharmaceuticals',), ('Privatization',),
    ('Public panic, Altruism',), ('QA rewrite: citizen activism,
    corporatism-environment, hope, inspiration',), ('Wal-Mart, Food
    Stamps',), ('Socially Responsible Investment, Banking and Environmental
    Initiatives',), ('Terrorism, Suicide Attacks',), ('Agriculture,
    Corporate v. Family Farming',), ('Depression, Health',), ('Depression,
    Health, Employment, Work, Employee',), ('Self-Interest, Vote,
    Altruism',), ('Animal Feed',), ('Corporate Reform (Dem. Book)',),
    ('Economic Growth',), ('Corporate History, Globalization',),
    ('Agriculture',), ('Environment, Public Opinion (Dem. Book)',),
    ('Illiteracy, poverty',), ('Military, Arms Sales',), ('Agriculture;
    Agricultural imports; Global trade',), ('Africa; Foreign aid; Third
    World debt; Loan forgiveness; Jubilee',), ('Foreign aid; Military aid;
    Development aid; Humanitarian aid',), ('Foreign aid; Development
    assistance',), ('Agricultural Subsidies; Foreign aid; Cotton subsidies;
    Africa',))
    >>>
     
    ronrsr, Nov 17, 2006
    #1
    1. Advertising

  2. ronrsr

    ronrsr Guest

    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.


    ronrsr wrote:
    > here is my result.
    > How do I determine the number of tuples in this array, returned from a
    > mysql database?
    >
    > How do I determine the number of characters or entry in each tuple?
    >
    > thanks very much for your assistance,
    >
    > -rsr-
    >
    >
    > (('Agricultural subsidies; Foreign aid',), ('Agriculture; Sustainable
    > Agriculture - Support; Organic Agriculture; Pesticides, US, Childhood
    > Development, Birth Defects; Toxic Chemicals',), ('Antibiotics,
    > Animals',), ('Agricultural Subsidies, Global Trade',), ('Agricultural
    > Subsidies',), ('Biodiversity',), ('Citizen Activism',), ('Community
    > Gardens',), ('Cooperatives',), ('Dieting',), ('Agriculture, Cotton',),
    > ('Agriculture, Global Trade',), ('Pesticides, Monsanto',),
    > ('Agriculture, Seed',), ('Coffee, Hunger',), ('Pollution, Water,
    > Feedlots',), ('Food Prices',), ('Agriculture, Workers',), ('Animal
    > Feed, Corn, Pesticides',), ('Aquaculture',), ('Chemical Warfare',),
    > ('Compost',), ('Debt',), ('Consumerism',), ('Fear',), ('Pesticides, US,
    > Childhood Development, Birth Defects',), ('Corporate Reform,
    > Personhood (Dem. Book)',), ('Corporate Reform, Personhood, Farming
    > (Dem. Book)',), ('Crime Rates, Legislation, Education',), ('Debt,
    > Credit Cards',), ('Democracy',), ('Population, World',), ('Income',),
    > ('Democracy, Corporate Personhood, Porter Township (Dem. Book)',),
    > ('Disaster Relief',), ('Dwellings, Slums',), ('Economics, Mexico',),
    > ('Economy, Local',), ('Education, Protests',), ('Endangered Habitat,
    > Rainforest',), ('Endangered Species',), ('Endangered Species,
    > Extinction',), ('antibiotics, livestock',), ('Pesticides, Water',),
    > ('Environment, Environmentalist',), ('Food, Hunger, Agriculture, Aid,
    > World, Development',), ('Agriculture, Cotton Trade',), ('Agriculture,
    > Cotton, Africa',), ('Environment, Energy',), ('Fair Trade (Dem.
    > Book)',), ('Farmland, Sprawl',), ('Fast Food, Globalization,
    > Mapping',), ('depression, mental illness, mood disorders',), ('Economic
    > Democracy, Corporate Personhood',), ('Brazil, citizen activism, hope,
    > inspiration, labor issues',), ('citizen activism, advice, hope',),
    > ('Pharmaceuticals, Medicine, Drugs',), ('Community Investing',),
    > ('Environment, Consumer Waste Reduction, Consumer Behavior and
    > Taxes',), ('Hunger, US, Poverty',), ('FERTILITY, Women',),
    > ('Corporatism, Environment',), ('Economic Democracy, Corporate
    > Farming',), ('Economic Democracy, Inspiration',), ('FEAR
    > (Fearlessness)',), ('Federal budget, military spending, foreign
    > cultural exchange programs',), ('Fish Farming',), ('Fish population,
    > commercial fishing industry',), ('GMO, BT Cotton',), ('Food borne
    > Illness, Water',), ('Food Charity',), ('Food Charity, Urban Farming',),
    > ('Food Consumption, Luxury',), ('Food Cost, international',), ('Food
    > Disposal, Waste',), ('Food stamps, eligibility for',), ('Food,
    > expenditure on',), ('Foreign Aid',), ('Genetics, Food, Agriculture,
    > International',), ('Global Policy, Bush Administration',), ('Global
    > Trade, food',), ('Global Trade, free trade, clothing',),
    > ('Globalization',), ('GMO, SACTO Protests',), ('GMO, Wheat, trade',),
    > ('Happiness and income',), ('Health Care - Spending',), ('Hope',),
    > ('Hope, inspirational quote',), ('Human Cloning',), ('Hunger',),
    > ('Hunger, income',), ('IMF, Argentina',), ('IMF, failings',),
    > ('Inequality',), ('INEQUALITY, GLOBALIZATION, POVERTY',), ('Inequality,
    > Globalization, Latin America',), ('Inspiration',), ('Junk Food',),
    > ('Junk Food, Soft Drinks',), ('Junk Food ,Children, Education,
    > Media',), ('Junk Food, Education, Children, Nutrition',), ('Junk Food,
    > Global, Coke',), ('Junk Food, McDonald\x92s',), ('Junk Food,
    > McDonald\x92s, Global',), ('Junk Food, Soft Drinks, Water',), ('Junk
    > Food, Education, Nutrition, Childhood Illness',), ('Junk food,
    > Nutrition, Schools',), ('Labor',), ('Labor Issues, Agriculture,
    > Sustainable Agriculture Support',), ('Labor Issues, Employee
    > Ownership',), ('Land Reform, Brazil, Infant mortality',),
    > ('Livestock',), ('Meat',), ('Meat, Brazil, Soybeans, Deforestation',),
    > ('Meat, concentration',), ('Meat, Recalls, Contamination',), ('Meat,
    > Recalls',), ('Media',), ('Microloans',), ('Military Spending',),
    > ('Nutrition Programs, School Meals, Snapple',), ('Nutrition,
    > Finland',), ('Obesity',), ('Obesity, Childhood',), ('Obesity,
    > Africa',), ('Organic Food',), ('Obesity, Global',), ('Obesity, related
    > illnesses',), ('Obesity, related illnesses, children',), ('Oil',),
    > ('Organic Agriculture',), ('Organic Agriculture, Growth',), ('Organic
    > Agriculture, Nutrition',), ('Organic Agriculture, Productivity',),
    > ('Organic food prices',), ('Community',), ('Corporate Concentration',),
    > ('Community Gardens, Urban Gardens',), ('Pollution, Air, CO2
    > emissions',), ('Population, Water',), ('Poverty, Aid',), ('Poverty,
    > World Bank, Hunger',), ('Organic Produce',), ('Organic Produce,
    > Farmer\x92s Markets, CSA Community Initiatives (Dem. Book)',),
    > ('Pakistan, Education, Poverty, Terrorism',), ('Pesticides',),
    > ('Pollution, US',), ('Population, Women',), ('Poverty, Labor Issues',),
    > ('t2',), ('Agriculture, Subsidies, Farming, Ranching, Government,
    > Livestock, Cattle',), ('School, Farmers\x92 Fresh Produce Food',),
    > ('School, Fast Food, Lunch Programs',), ('School, Fast Food, Lunch
    > Programs, Harms',), ('Citizens Groups',), ('Public Health, Bacteria',),
    > ('Violence, Impotence',), ('Private Food Aid, Food Stamps',), ('Prison
    > Rates, Race',), ('Produce, Nutritional content of produce, Historical
    > Perspective',), ('Public Opinion',), ('Slavery',), ('Solar, Renewable
    > Energy',), ('Suicide',), ('Sustainable agriculture, Moringa tree',),
    > ('Sweatshop Labor',), ('Tax Cuts, George W. Bush',), ('Economic
    > Democracy (Dem. Book)',), ('Tobacco',), ('Toxic Chemicals',),
    > ('Trade',), ('Trade, Agricultural Raw materials',), ('Trade, Coffee',),
    > ('Trade, Cotton',), ('Trade, NAFTA, Mexico',), ('Vegetarianism',),
    > ('Veterans\x92 Benefits',), ('Violence',), ('Violence, Media,
    > Children',), ('Water',), ('Water, Privatization',), ('Welfare
    > Reform',), ('Wind, Renewable Energy',), ('Words',), ('World
    > Population',), ('World Bank',), ('Grassroots, Community, Living Wage,
    > Poverty',), ('Subsidies',), ('Protest, Action',), ('Nuclear Power
    > Plants, Emissions',), ('Media, Fear, Religion, George W. Bush',),
    > ('Consciousness, Change',), ('Peace, Security, Oil',), ('Globalization,
    > Inequality, Commodity prices',), ('Water, Corporate',), ('Consumerism,
    > Developing Nations',), ('Nuclear Power, Wind Power',),
    > ('Sustainability, Hope',), ('Ozone, CFCs, Chlorine, Atmosphere',),
    > ('Hydrogen, Emissions, Ozone',), ('Water, Pesticides',), ('Iraq,
    > Military, Health',), ('Toxic Waste Reduction, Toxic Waste Disposal,
    > Environmental Regulation, Recycling Electronics',), ('Shareholder
    > Advocacy, Social Responsibility, Community Investing (Dem. Book)',),
    > ('Fair Trade',), ('Genetically Modified Food, Africa, USAID, ISAAA',),
    > ('Sustainability, Progress',), ('Sustainability, Human Rights,
    > Globalization, Corporatization',), ('Agricultural Subsidies,
    > Agricultural Policy',), ('Agricultural Subsidies, Farmers',), ('Cotton,
    > Rice',), ('Agricultural Subsidies, Farmers, Livestock',),
    > ('Agricultural Subsidies, Prices',), ('Food scarcity, Hunger, Urban
    > Agriculture',), ('GMO farming, Toxic chemicals',), ('Hunger,
    > Children',), ('Hunger, Food Insecurity',), ('Inspiration, Corporatism,
    > War',), ('Inspiration, Economic Growth',), ('Land Vacancies, US Land
    > Reform',), ('Local agriculture, Local produce, Nutritional content of
    > local produce',), ('Local produce, Urban agriculture',), ('Pakistan,
    > Religion, Education',), ('Pharmaceuticals',), ('Privatization',),
    > ('Public panic, Altruism',), ('QA rewrite: citizen activism,
    > corporatism-environment, hope, inspiration',), ('Wal-Mart, Food
    > Stamps',), ('Socially Responsible Investment, Banking and Environmental
    > Initiatives',), ('Terrorism, Suicide Attacks',), ('Agriculture,
    > Corporate v. Family Farming',), ('Depression, Health',), ('Depression,
    > Health, Employment, Work, Employee',), ('Self-Interest, Vote,
    > Altruism',), ('Animal Feed',), ('Corporate Reform (Dem. Book)',),
    > ('Economic Growth',), ('Corporate History, Globalization',),
    > ('Agriculture',), ('Environment, Public Opinion (Dem. Book)',),
    > ('Illiteracy, poverty',), ('Military, Arms Sales',), ('Agriculture;
    > Agricultural imports; Global trade',), ('Africa; Foreign aid; Third
    > World debt; Loan forgiveness; Jubilee',), ('Foreign aid; Military aid;
    > Development aid; Humanitarian aid',), ('Foreign aid; Development
    > assistance',), ('Agricultural Subsidies; Foreign aid; Cotton subsidies;
    > Africa',))
    > >>>
     
    ronrsr, Nov 17, 2006
    #2
    1. Advertising

  3. 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:

    >> (('Agricultural subsidies; Foreign aid',), ('Agriculture; Sustainable
    >> Agriculture - Support; Organic Agriculture; Pesticides, US, Childhood
    >> Development, Birth Defects; Toxic Chemicals',),


    </F>
     
    Fredrik Lundh, Nov 17, 2006
    #3
  4. ronrsr

    ronrsr Guest

    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.

    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.

    Each entry in that list is a keyword - Alternately, is there any fast
    way to parse that into a sorted list of distinct keywords.

    very sorry for the error.

    bests,

    -rsr-

    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:
    >
    > >> (('Agricultural subsidies; Foreign aid',), ('Agriculture; Sustainable
    > >> Agriculture - Support; Organic Agriculture; Pesticides, US, Childhood
    > >> Development, Birth Defects; Toxic Chemicals',),

    >
    > </F>
     
    ronrsr, Nov 17, 2006
    #4
  5. ronrsr

    John Machin Guest

    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:
    > >
     
    John Machin, Nov 17, 2006
    #5
  6. On 16 Nov 2006 23:01:39 -0800, "ronrsr" <> declaimed the
    following in comp.lang.python:

    > 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.
    >
    > 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(result) # number of tuples in result set
    len(result[x]) # number of entries in tuple "x"
    len(result[x][y]) # length of entry "y" in tuple "x"

    >>> result = (('Agricultural subsidies; Foreign aid',), ('Agriculture; Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US, Childhood Development, Birth Defects; Toxic Chemicals',),('Antibiotics, Animals',), ('Agricultural Subsidies, Global Trade',), ('Agricultural Subsidies',))
    >>> len(result)

    5
    >>> len(result[0])

    1
    >>> len(result[0][0])

    35
    >>> for t in result:

    .... for s in t:
    .... print len(s), s
    .... print
    ....
    35 Agricultural subsidies; Foreign aid

    138 Agriculture; Sustainable Agriculture - Support; Organic Agriculture;
    Pesticides, US, Childhood Development, Birth Defects; Toxic Chemicals

    20 Antibiotics, Animals

    36 Agricultural Subsidies, Global Trade

    22 Agricultural Subsidies

    >>>


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

    Why not start by asking MySQL to do the sorting?
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Nov 17, 2006
    #6
  7. ronrsr wrote:

    > 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.


    from python's perspective, the data structure you're looking at looks
    like

    tuple of
    tuple of
    one string
    tuple of
    one string
    tuple of
    one string
    etc

    so len(result) is the number of tuples, and len(result[index]) is the
    number of strings in the inner tuples (=1). to get at an individual
    string, do

    s = result[index][0]

    to look inside the string, you need to apply the appropriate string
    operations to extract the data, or split it up in some suitable way.
    there's no way Python can figure out how a string appears to you; from
    Python's perspective, it's just a bunch of characters.

    </F>
     
    Fredrik Lundh, Nov 17, 2006
    #7
  8. ronrsr

    Guest

    ronrsr wrote:
    > 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.


    One string composed of multiple data elements means
    the problem is in your database design, not in your Python code.

    >
    > 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?


    You have to split the strings based on some delimiter, but your
    delimiters seem inconsistent. Unless the different delimiters actually
    have different meanings which, again, implies bad database design.

    > so that I
    > can iterate through them.
    >
    > Each entry in that list is a keyword - Alternately, is there any fast
    > way to parse that into a sorted list of distinct keywords.


    That could easily be done in the database itself, but only if it is
    designed properly.

    >
    > very sorry for the error.
    >
    > bests,
    >
    > -rsr-
    >
    > 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:
    > >
    > > >> (('Agricultural subsidies; Foreign aid',), ('Agriculture; Sustainable
    > > >> Agriculture - Support; Organic Agriculture; Pesticides, US, Childhood
    > > >> Development, Birth Defects; Toxic Chemicals',),

    > >
    > > </F>
     
    , Nov 17, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    812
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    960
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    615
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    826
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    970
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page