Just a little fun with Python's named tuple, behaves like a class but is much leaner.
US states named tuple (Python)
''' namedtuple_US_states1.py
use a data string (potential csv file with ; separator)
to create a named tuple of US states
tested with Python27 and Python33 by vegaseat 17sep2013
'''
from collections import namedtuple
import pprint
# each line (use ; as separator since date contains a comma)
# state;capital;joined union at date;flower;bird
data_str = """\
Alabama;Montgomery;December 14, 1819;Camellia;Yellowhammer
Alaska;Juneau;January 3, 1959;Forget-me-not;Willow Ptarmigan
Arizona;Phoenix;February 14, 1912;Suguaro Cactus Blossom;Cactus Wren
Arkansas;Little Rock;June 15, 1836;Apple Blossom;Mockingbird
California;Sacremento;September 9, 1850;Golden Poppy;California Valley Quail
Colorado;Denver;August 1, 1876;Mountain Columbine;Lark Bunting
Connecticut;Hartford;January 9, 1788;Mountain Laurel;Robin
Delaware;Dover;December 7, 1787;Peach Blossom;Blue Hen Chicken
Florida;Tallahassee;March 3, 1845;Orange Blossom;Mockingbird
Georgia;Atlanta;January 2, 1788;Cherokee Rose;Brown Thrasher
Hawaii;Honolulu;August 21, 1959;Red Hibiscus;Nene (Hawaiian Goose)
Idaho;Boise;July 3, 1890;Syringa;Mountain Bluebird
Illinois;Springfield;December 3, 1818;Violet;Cardinal
Indiana;Indianapolis;December 11, 1816;Peony;Cardinal
Iowa;Des Moines;December 28, 1846;Wild Rose;Eastern Goldfinch
Kansas;Topeka;January 29, 1861;Sunflower;Western Meadowlark
Kentucky;Frankfort;June 1, 1792;Goldenrod;Cardinal
Louisiana;Baton Rouge;April 30, 1812;Magnolia;Eastern Brown Pelican
Maine;Augusta;March 15, 1820;Pine Cone & Tassel;Chickadee
Maryland;Annapolis;April 28, 1788;Black-eyed Susan;Baltimore Oriole
Massachusetts;Boston;February 6, 1788;Mayflower;Chickadee
Michigan;Lansing;January 26, 1837;Apple Blossom;Robin
Minnesota;St. Paul;May 11, 1858;Lady-slipper;Loon
Mississippi;Jackson;December 10, 1817;Magnolia;Mockingbird
Missouri;Jefferson City;August 10, 1821;Hawthorn;Bluebird
Montana;Helena;November 8, 1889;Bitterroot;Western Meadowlark
Nebraska;Lincoln;March 1, 1867;Goldenrod;Western Meadowlark
Nevada;Carson City;October 31, 1864;Sagebrush;Mountain Bluebird
New Hampshire;Concord;June 21, 1788;Purple Lilac;Purple Finch
New Jersey;Trenton;December 18, 1787;Violet;Eastern Goldfinch
New Mexico;Santa Fe;January 6, 1912;Yucca;Road Runner
New York;Albany;July 26, 1788;Rose;Bluebird
North Carolina;Raleigh;November 21, 1789;Flowering Dogwood;Cardinal
North Dakota;Bismarck;November 2, 1889;Prairie Rose;Meadowlark
Ohio;Columbus;March 1, 1803;Scarlet Carnation;Cardinal
Oklahoma;Oklahoma City;November 16, 1907;Mistletoe;Scissor-tailed Flycatcher
Oregon;Salem;February 14, 1859;Oregon Grape;Western Meadowlark
Pennsylvania;Harrisburg;December 12, 1787;Mountain Laurel;Ruffed Grouse
Rhode Island;Providence;May 29, 1790;Violet;Rhode Island Red
South Carolina;Columbia;May 23, 1788;Yellow Jessamine;Carolina Wren
South Dakota;Pierre;November 2, 1889;Pasqueflower;Ring-necked Pheasant
Tennessee;Nashville;June 1, 1796;Iris;Mockingbird
Texas;Austin;December 29, 1845;Bluebonnet;Mockingbird
Utah;Salt Lake City;January 4, 1896;Sego Lily;Sea Gull
Vermont;Montpelier;March 4, 1791;Red Clover;Hermit Thrush
Virginia;Richmond;June 26, 1788;Dogwood;Cardinal
Washington;Olympia;November 11, 1889;Coast Rhododendron;Willow Goldfinch
West Virginia;Charleston;June 20, 1863;Rhododendron;Cardinal
Wisconsin;Madison;May 29, 1848;Wood Violet;Robin
Wyoming;Cheyenne;July 10, 1890;Indian Paintbrush;Meadowlark
"""
# create a list of [state, capital, date, flower, bird] lists
# sort by state name just to make sure
data_list = sorted(line.split(';') for line in data_str.split('\n') if line)
#pprint.pprint(data_list) # test
# named tuple States behaves like a class, and has much less overhead
States = namedtuple('States', 'state capital date flower bird')
# create a list of instances
instance_list = [States(state, capital, date, flower, bird)\
for state, capital, date, flower, bird in data_list]
#pprint.pprint(instance_list) # test
#print('-'*35)
# show the states where the state bird is a "Robin"
bird = "Robin"
print("States with {} as state bird:".format(bird))
for state in instance_list:
if state.bird == bird:
print(state.state)
print('-'*35)
# show the states where the state flower is a "Apple Blossom"
flower = "Apple Blossom"
print("States with {} as state flower:".format(flower))
for state in instance_list:
if state.flower == flower:
print(state.state)
print('-'*35)
first = 'M'
print("All the states that start with '{}':".format(first))
for state in instance_list:
if state.state[0] == first:
print(state.state)
print('-'*35)
print("All US states and their capitals:")
for state in instance_list:
print("{:15} {}".format(state.state, state.capital))
''' result ...
States with Robin as state bird:
Connecticut
Michigan
Wisconsin
-----------------------------------
States with Apple Blossom as state flower:
Arkansas
Michigan
-----------------------------------
All the states that start with 'M':
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
-----------------------------------
All US states and their capitals:
Alabama Montgomery
Alaska Juneau
Arizona Phoenix
Arkansas Little Rock
California Sacremento
Colorado Denver
Connecticut Hartford
Delaware Dover
Florida Tallahassee
Georgia Atlanta
Hawaii Honolulu
Idaho Boise
Illinois Springfield
Indiana Indianapolis
Iowa Des Moines
Kansas Topeka
Kentucky Frankfort
Louisiana Baton Rouge
Maine Augusta
Maryland Annapolis
Massachusetts Boston
Michigan Lansing
Minnesota St. Paul
Mississippi Jackson
Missouri Jefferson City
Montana Helena
Nebraska Lincoln
Nevada Carson City
New Hampshire Concord
New Jersey Trenton
New Mexico Santa Fe
New York Albany
North Carolina Raleigh
North Dakota Bismarck
Ohio Columbus
Oklahoma Oklahoma City
Oregon Salem
Pennsylvania Harrisburg
Rhode Island Providence
South Carolina Columbia
South Dakota Pierre
Tennessee Nashville
Texas Austin
Utah Salt Lake City
Vermont Montpelier
Virginia Richmond
Washington Olympia
West Virginia Charleston
Wisconsin Madison
Wyoming Cheyenne
'''
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
BearofNH 104 Posting Whiz
BearofNH 104 Posting Whiz
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
BearofNH 104 Posting Whiz
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.