Skip to main content

USA cities



In the last post we made a test with the capitals. Now I want to do a test were you have to find the capitals among the city of the same state. But I need the data and I don't want to write them all manually, so I found this data on the net, but I need to put them in a dictonary in order to use them. Let's solve this.

We have a text file like this (city.txt):

pSitka;Alaska
Juneau;Alaska
Wrangell;Alaska
Anchorage;Alaska
Jacksonville;Florida
Anaconda;Montana
Butte;Montana
Oklahoma City;Oklahoma
Houston;Texas
Phoenix;Arizona
Nashville;Tennessee
Los Angeles;California
San Antonio;Texas
Suffolk;Virginia
Buckeye;Arizona
Indianapolis;Indiana
Chesapeake;Virginia
Dallas;Texas
Fort Worth;Texas
Louisville;Kentucky
San Diego;California
Memphis;Tennessee
Kansas City;Missouri
New York City;New York
Augusta;Georgia
Austin;Texas
Charlotte;North Carolina
Lexington;Kentucky
El Paso;Texas
Macon;Georgia
Virginia Beach;Virginia
Cusseta;Georgia
Chicago;Illinois
Tucson;Arizona
Columbus;Ohio
Columbus;Georgia
Valdez;Alaska
Preston;Georgia
Huntsville;Alabama
Boulder City;Nevada
California City;California
Tulsa;Oklahoma
Colorado Springs;Colorado
Goodyear;Arizona
Albuquerque;New Mexico
Scottsdale;Arizona
Hibbing;Minnesota
Norman;Oklahoma
San Jose;California
Peoria;Arizona
New Orleans;Louisiana
Corpus Christi;Texas
Montgomery;Alabama
Wichita;Kansas
Aurora;Colorado
Denver;Colorado
Sierra Vista;Arizona
Georgetown;Georgia
Birmingham;Alabama
Fayetteville;North Carolina
Carson City;Nevada
Raleigh;North Carolina
Bakersfield;California
Mobile;Alabama
Detroit;Michigan
Bunnell;Florida
Mesa;Arizona
Las Vegas;Nevada
Chattanooga;Tennessee
Philadelphia;Pennsylvania
Portland;Oregon
Atlanta;Georgia
Winston-Salem;North Carolina
Brownsville;Texas
Columbia;South Carolina
Lynchburg;Tennessee
Marana;Arizona
Yuma;Arizona
Athens;Georgia
Little Rock;Arkansas
Omaha;Nebraska
Lubbock;Texas
Tampa;Florida
Eloy;Arizona
Unalaska;Alaska
Orlando;Florida
Salt Lake City;Utah
Henderson;Nevada
Surprise;Arizona
Babbitt;Minnesota
Cape Coral;Florida
Abilene;Texas
Palmdale;California
Jackson;Mississippi
Greensboro;North Carolina
Fresno;California
Shreveport;Louisiana
North Las Vegas;Nevada
St. Marys;Pennsylvania
Sacramento;California
Charleston;South Carolina
Nightmute;Alaska
Plymouth;Massachusetts
Milwaukee;Wisconsin
Arlington;Texas
Tallahassee;Florida
Clarksville;Tennessee
Durham;North Carolina
Palm Springs;California
Lancaster;California
Knoxville;Tennessee
Laredo;Texas
Amarillo;Texas
Dothan;Alabama
Oak Ridge;Tennessee
Edmond;Oklahoma
Beaumont;Texas
Waco;Texas
Seattle;Washington
Port Arthur;Texas
Baltimore;Maryland
Toledo;Ohio
Kansas City;Kansas
El Reno;Oklahoma
Jonesboro;Arkansas
Ellsworth;Maine
Caribou;Maine
Fort Wayne;Indiana
Independence;Missouri
Riverside;California
Cincinnati;Ohio
Las Cruces;New Mexico
Cleveland;Ohio
Baton Rouge;Louisiana
Fremont;California
Presque Isle;Maine
Des Moines;Iowa
Port St. Lucie;Florida
Lawton;Oklahoma
Rome;New York
North Port;Florida
Savannah;Georgia
Lincoln;Nebraska
Enid;Oklahoma
Rio Rancho;New Mexico
Apple Valley;California
Springfield;Missouri
Victorville;California
Plano;Texas
Grand Prairie;Texas

Wichita Falls;Texas

Where we have cities on left and states on right.
We want to create a dictonary, were we have states as key and all the city belonging to this states as values separated by comma.
Let's go for code:

1. CAPTURE DATA


with open("city.txt") as file:
    city = file.readlines()


2. ERASE THE \n AT THE END OF THE LINES


for c in city:
    city[city.index(c)] = c.replace("\n","")


3. CREATE THE DICTONARY WITH THE KEYS (STATES)


cityd = {}
for c in city:
    c = c.split(";")
    cityd[c[1]] = ""


4. ADD THE CITIES AS VALUES


for c in city:
    c = c.split(";")
    cityd[c[1]] += c[0] +","


5. SHOW RESULTS


for c in cityd:
    print(c,"\n",cityd[c],"\n")

THE CODE TOGETHER AND
 THE
OUTPUT


with open("city.txt") as file:
    city = file.readlines()

for c in city:
    city[city.index(c)] = c.replace("\n","")

cityd = {}
for c in city:
    c = c.split(";")
    cityd[c[1]] = ""

for c in city:
    c = c.split(";")
    cityd[c[1]] += c[0] +","

for c in cityd:
    print(c,"\n",cityd[c],"\n")
New York 
 New York City,Rome, 

Utah 
 Salt Lake City, 

Maine 
 Ellsworth,Caribou,Presque Isle, 

Indiana 
 Indianapolis,Fort Wayne, 

Mississippi 
 Jackson, 

Maryland 
 Baltimore, 

Tennessee 
 Nashville,Memphis,Chattanooga,Lynchburg,Clarksville,Knoxville,Oak Ridge, 

Oregon 
 Portland, 

California 
 Los Angeles,San Diego,California City,San Jose,Bakersfield,Palmdale,Fresno,Sacramento,Palm Springs,Lancaster,Riverside,Fremont,Apple Valley,Victorville, 

Illinois 
 Chicago, 

Kentucky 
 Louisville,Lexington, 

Georgia 
 Augusta,Macon,Cusseta,Columbus,Preston,Georgetown,Atlanta,Athens,Savannah, 

Colorado 
 Colorado Springs,Aurora,Denver, 

South Carolina 
 Columbia,Charleston, 

Alaska 
 pSitka,Juneau,Wrangell,Anchorage,Valdez,Unalaska,Nightmute, 

Washington 
 Seattle, 

Arizona 
 Phoenix,Buckeye,Tucson,Goodyear,Scottsdale,Peoria,Sierra Vista,Mesa,Marana,Yuma,Eloy,Surprise, 

New Mexico 
 Albuquerque,Las Cruces,Rio Rancho, 

Virginia 
 Suffolk,Chesapeake,Virginia Beach, 

Louisiana 
 New Orleans,Shreveport,Baton Rouge, 

Kansas 
 Wichita,Kansas City, 

Montana 
 Anaconda,Butte, 

Wisconsin 
 Milwaukee, 

Nevada 
 Boulder City,Carson City,Las Vegas,Henderson,North Las Vegas, 

Arkansas 
 Little Rock,Jonesboro, 

Florida 
 Jacksonville,Bunnell,Tampa,Orlando,Cape Coral,Tallahassee,Port St. Lucie,North Port, 

Alabama 
 Huntsville,Montgomery,Birmingham,Mobile,Dothan, 

Nebraska 
 Omaha,Lincoln, 

Iowa 
 Des Moines, 

Texas 
 Houston,San Antonio,Dallas,Fort Worth,Austin,El Paso,Corpus Christi,Brownsville,Lubbock,Abilene,Arlington,Laredo,Amarillo,Beaumont,Waco,Port Arthur,Plano,Grand Prairie,Wichita Falls, 

North Carolina 
 Charlotte,Fayetteville,Raleigh,Winston-Salem,Greensboro,Durham, 

Minnesota 
 Hibbing,Babbitt, 

Michigan 
 Detroit, 

Ohio 
 Columbus,Toledo,Cincinnati,Cleveland, 

Oklahoma 
 Oklahoma City,Tulsa,Norman,Edmond,El Reno,Lawton,Enid, 

Pennsylvania 
 Philadelphia,St. Marys, 

Massachusetts 
 Plymouth, 

Missouri 
 Kansas City,Independence,Springfield, 


Comments

Popular posts from this blog

Widgets for Jupyter Notebook: a text input widget

Widgets for Jupyter notebook ¶ Let's import the module ipywidgets into the Jupyter Notebook from ipywidgets import widgets from ipywidgets import * from traitlets import * Now we import the display function from IPython ¶ let's attach a function to the event on_submit After we run this cell, we can go up and write something in the text widget and after you submit the text you wrote will be printed after the cell from IPython.display import display text = widgets . Text () display ( text ) def handle_submit ( sender ): print ( "Thank you for entering this text:" , text . value ) text . on_submit ( handle_submit ) Thank you for entering this text: Ciao

Image in Jupyter and PIL step by step

Hi, """ Hi, we will see a step by step tutorial about PIL and IPython.core.display modules to create images from other images and diplaying them in Jupyter notebook """ # What we will do # Create a card # 1. Take a pic of a heart # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle # 4. show the card """ As first step wi will simply display an image on the notebook. I will show two way to display the image with 'display' from IPhyton a. Using the open method of PIL.Image (named Img) b. Using the Image method from the IPython.core.display module """ # 1. Take the pic of a heart from IPython.core.display import Image , display from PIL import Image as Img heart = 'img/heart.png' display ( Image ( heart )) display ( Img . open ( heart )) # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle #...

Let's draw a circle with PIL in Python

Let's continue making our coding around PIL. Let's start with some basic drawing: a circle from PIL import Image , ImageDraw img = Image . new ( "RGB" ,( 60 , 60 ), 'white' ) dr = ImageDraw . Draw ( img ) dr . ellipse (( 0 , 0 , 60 , 60 ), 'yellow' ) img . show () this is the image produced *If you use jupyter notebook, just write img at the end to see the output.