Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
1_States_Capitals
#1
Hi, I am a complete newbie for Python, and tried to code this program that finds Capitals of the States. It works perfectly fine, provides the desired output. But as a novice, I believe it is too large, is there any possibility to make is shorter, simpler because my program looks huge. Please take a look in my codes and let me know how can it be made better, effective and resourceful. Advices will be heartily appreciated. Thank you in advance.

x = input("Please enter State: ")

States =['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio',
'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia',
'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']

Abb = ['AL', 'AS', 'AZ', 'AK', 'CA', 'CO', 'CT', 'DL', 'FL', 'GA', 'HA', 'IA', 'IL', 'IN', 'IO', 'KS', 'KY','LO', 'MA', 'MD',
       'MC', 'MI', 'MN', 'MS', 'MO', 'MT', 'NB', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR',
       'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT','VT', 'VA', 'WA', 'WV', 'WN', 'WY']

Capitals = ['Montgomery', 'Juneau', 'Phoenix', 'Little Rock', 'Sacramento', 'Denver', 'Hartford', 'Dover', 'Tallahassee', 'Atlanta', 'Honolulu',
'Boise', 'Springfield', 'Indianapolis', 'Des Moines', ', Topeka', 'Frankfort', 'Baton Rouge', 'Augusta', 'Annapolis', 'Boston', 'Lansing', 'Saint Paul', 'Jackson',
'Jefferson City', 'Helena', 'Lincoln', 'Carson City', 'Concord', 'Trenton', 'Santa Fe', 'Albany', 'Raleigh', 'Bismarck', 'Columbus', 'Oklahoma City',
'Salem', 'Harrisburg', 'Providence', 'Columbia', 'Pierre', 'Nashville', 'Austin', 'Salt Lake City', 'Montpelier', 'Richmond', 'Olympia', 'Charleston', 'Madison',
'Cheyenne']

if x == States [0] or x == Abb[0]:
    print (Capitals[0])
if x == States [1] or x == Abb[1]:
        print (Capitals[1])
if x == States [2] or x == Abb[2]:
    print (Capitals[2])
if x == States [3] or x == Abb[3]:
    print (Capitals[3])
if x == States [4] or x == Abb[4]:
    print (Capitals[4])
if x == States [5] or x == Abb[5]:
    print (Capitals[5])
if x == States [6] or x == Abb[6]:
    print (Capitals[6])
if x == States [7] or x == Abb[7]:
    print (Capitals[7])
if x == States [8] or x == Abb[8]:
    print (Capitals[8])
if x == States [9] or x == Abb[9]:
    print (Capitals[9])
if x == States [10] or x == Abb[10]:
    print (Capitals[10])
if x == States [11] or x == Abb[11]:
    print (Capitals[11])
if x == States [12] or x == Abb[12]:
    print (Capitals[12])
if x == States [13] or x == Abb[13]:
    print (Capitals[13])
if x == States [14] or x == Abb[14]:
    print (Capitals[14])
if x == States [15] or x == Abb[15]:
    print (Capitals[15])
if x == States [16] or x == Abb[16]:
    print (Capitals[16])
if x == States [17] or x == Abb[17]:
    print (Capitals[17])
if x == States [18] or x == Abb[18]:
    print (Capitals[18])
if x == States [19] or x == Abb[19]:
    print (Capitals[19])
if x == States [20] or x == Abb[20]:
    print (Capitals[20])
if x == States [21] or x == Abb[21]:
    print (Capitals[21])
if x == States [22] or x == Abb[22]:
    print (Capitals[22])
if x == States [23] or x == Abb[23]:
    print (Capitals[23])
if x == States [24] or x == Abb[24]:
    print (Capitals[24])
if x == States [25] or x == Abb[25]:
    print (Capitals[25])
if x == States [26] or x == Abb[26]:
    print (Capitals[26])
if x == States [27] or x == Abb[27]:
    print (Capitals[27])
if x == States [28] or x == Abb[28]:
    print (Capitals[28])
if x == States [29] or x == Abb[29]:
    print (Capitals[29])
if x == States [30] or x == Abb[30]:
    print (Capitals[30])
if x == States [31] or x == Abb[31]:
    print (Capitals[31])
if x == States [32] or x == Abb[32]:
    print (Capitals[32])
if x == States [33] or x == Abb[33]:
    print (Capitals[33])
if x == States [34] or x == Abb[34]:
    print (Capitals[34])
if x == States [35] or x == Abb[35]:
    print (Capitals[35])
if x == States [36] or x == Abb[36]:
    print (Capitals[36])
if x == States [37] or x == Abb[37]:
    print (Capitals[37])
if x == States [38] or x == Abb[38]:
    print (Capitals[38])
if x == States [39] or x == Abb[39]:
    print (Capitals[39])
if x == States [40] or x == Abb[40]:
    print (Capitals[40])
if x == States [41] or x == Abb[41]:
    print (Capitals[41])
if x == States [42] or x == Abb[42]:
    print (Capitals[42])
if x == States [43] or x == Abb[43]:
    print (Capitals[43])
if x == States [44] or x == Abb[44]:
    print (Capitals[44])
if x == States [45] or x == Abb[45]:
    print (Capitals[45])
if x == States [46] or x == Abb[46]:
    print (Capitals[46])
if x == States [47] or x == Abb[47]:
    print (Capitals[47])
if x == States [48] or x == Abb[48]:
    print (Capitals[48])
if x == States [49] or x == Abb[49]:
    print (Capitals[49])
Reply
#2
this is perfect use case for dictionary.
Have a look at our tutorial on dcits
also read if structure converted to dictionary
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Agree with Buran about dictionary. If you are not yet to that level, substitute
for i in range(0,50) :
    if x==States[i] or x == Abb[i] :
        print(Capitals[i])
for the whole set of lines 19 to 118. 3 lines instead of 100.

As you learn more, you will see how restructuring your data as dictionaries instead of lists would be helpful here.
Reply
#4
I new to python too. I used what you posted fro an exercise and here is what I came up with. Maybe something you can tweak and get what you need.
States =['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio',
'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia',
'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']
 
Abb = ['AL', 'AS', 'AZ', 'AK', 'CA', 'CO', 'CT', 'DL', 'FL', 'GA', 'HA', 'IA', 'IL', 'IN', 'IO', 'KS', 'KY','LO', 'MA', 'MD',
       'MC', 'MI', 'MN', 'MS', 'MO', 'MT', 'NB', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR',
       'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT','VT', 'VA', 'WA', 'WV', 'WN', 'WY']
 
Capitals = ['Montgomery', 'Juneau', 'Phoenix', 'Little Rock', 'Sacramento', 'Denver', 'Hartford', 'Dover', 'Tallahassee', 'Atlanta', 'Honolulu',
'Boise', 'Springfield', 'Indianapolis', 'Des Moines', ', Topeka', 'Frankfort', 'Baton Rouge', 'Augusta', 'Annapolis', 'Boston', 'Lansing', 'Saint Paul', 'Jackson',
'Jefferson City', 'Helena', 'Lincoln', 'Carson City', 'Concord', 'Trenton', 'Santa Fe', 'Albany', 'Raleigh', 'Bismarck', 'Columbus', 'Oklahoma City',
'Salem', 'Harrisburg', 'Providence', 'Columbia', 'Pierre', 'Nashville', 'Austin', 'Salt Lake City', 'Montpelier', 'Richmond', 'Olympia', 'Charleston', 'Madison',
'Cheyenne']

lst = {}
j = 0
for i in States:
    lst[i] = Capitals[j]
    #print(i +  " " + Capitals[j])
    j += 1



x = input("Type a state > ")
x = x.capitalize()
if x in lst:
    print("{} - {}" .format(lst[x], x))
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
One more version to include the state abbrevations
strdict = {}
for i in range(50):
    strdict[i] = (States[i],Capitals[i], Abb[i])

x = input("Type state or abrv> ")
for i in range(len(strdict)):
    if x.capitalize() in strdict[i] or x.upper() in strdict[i]:
        print("Capital: {}, State: {}, State Abbrviation: {}".format(strdict[i][0], strdict[i][1],  strdict[i][2]))
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#6
@menator01, your latest snippet effectively makes the use of dictionary meaningless. The idea of using dict is to retrieve value by key, without need to iterate over the dictionary.

states = {'AL':'Alabama', 'AS':'Alaska', 'AZ':'Arizona', 'AK':'Arkansas', 
          'CA':'California', 'CO':'Colorado', 'CT':'Connecticut', 'DL':'Delaware', 
          'FL':'Florida', 'GA':'Georgia', 'HA':'Hawaii', 'IA':'Idaho', 'IL':'Illinois', 
          'IN':'Indiana', 'IO':'Iowa', 'KS':'Kansas', 'KY':'Kentucky', 'LO':'Louisiana', 
          'MA':'Maine', 'MD':'Maryland', 'MC':'Massachusetts', 'MI':'Michigan', 'MN':'Minnesota', 
          'MS':'Mississippi', 'MO':'Missouri', 'MT':'Montana', 'NB':'Nebraska', 'NV':'Nevada', 
          'NH':'New Hampshire', 'NJ':'New Jersey', 'NM':'New Mexico', 'NY':'New York', 
          'NC':'North Carolina', 'ND':'North Dakota', 'OH':'Ohio', 'OK':'Oklahoma', 'OR':'Oregon', 
          'PA':'Pennsylvania', 'RI':'Rhode Island', 'SC':'South Carolina', 'SD':'South Dakota', 
          'TN':'Tennessee', 'TX':'Texas', 'UT':'Utah', 'VT':'Vermont', 'VA':'Virginia', 
          'WA':'Washington', 'WV':'West Virginia', 'WN':'Wisconsin', 'WY':'Wyoming'}

capitals = {'Alabama':'Montgomery', 'Alaska':'Juneau', 'Arizona':'Phoenix', 'Arkansas':'Little Rock', 
            'California':'Sacramento', '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':'Saint 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'}

while True:
    state = input('Enter state:')
    if state:
        state = states.get(state.upper(), state)
        try:
            print(f'Capital of {state} is {capitals[state]}')
        except KeyError:
            print(f'{state} is not US state!')
    else:
        break
Output:
Enter state:tx Capital of Texas is Austin Enter state:California Capital of California is Sacramento Enter state:Bavaria Bavaria is not US state! Enter state:
you can combine both dicts in one, e.g.

{'AL': 'Montgomery', 'Alabama': 'Montgomery', 'AS': 'Juneau', 'Alaska': 'Juneau', 'AZ': 'Phoenix', 'Arizona': 'Phoenix', 
'AK': 'Little Rock', 'Arkansas': 'Little Rock', 'CA': 'Sacramento', 'California': 'Sacramento', 'CO': 'Denver', 'Colorado': 'Denver', 
'CT': 'Hartford', 'Connecticut': 'Hartford', 'DL': 'Dover', 'Delaware': 'Dover', 'FL': 'Tallahassee', 'Florida': 'Tallahassee', 
'GA': 'Atlanta', 'Georgia': 'Atlanta', 'HA': 'Honolulu', 'Hawaii': 'Honolulu', 'IA': 'Boise', 'Idaho': 'Boise', 'IL': 'Springfield', 
'Illinois': 'Springfield', 'IN': 'Indianapolis', 'Indiana': 'Indianapolis', 'IO': 'Des Moines', 'Iowa': 'Des Moines', 'KS': 'Topeka', 
'Kansas': 'Topeka', 'KY': 'Frankfort', 'Kentucky': 'Frankfort', 'LO': 'Baton Rouge', 'Louisiana': 'Baton Rouge', 'MA': 'Augusta', 
'Maine': 'Augusta', 'MD': 'Annapolis', 'Maryland': 'Annapolis', 'MC': 'Boston', 'Massachusetts': 'Boston', 'MI': 'Lansing', 
'Michigan': 'Lansing', 'MN': 'Saint Paul', 'Minnesota': 'Saint Paul', 'MS': 'Jackson', 'Mississippi': 'Jackson', 'MO': 'Jefferson City', 
'Missouri': 'Jefferson City', 'MT': 'Helena', 'Montana': 'Helena', 'NB': 'Lincoln', 'Nebraska': 'Lincoln', 'NV': 'Carson City', 
'Nevada': 'Carson City', 'NH': 'Concord', 'New Hampshire': 'Concord', 'NJ': 'Trenton', 'New Jersey': 'Trenton', 
'NM': 'Santa Fe', 'New Mexico': 'Santa Fe', 'NY': 'Albany', 'New York': 'Albany', 'NC': 'Raleigh', 'North Carolina': 'Raleigh', 
'ND': 'Bismarck', 'North Dakota': 'Bismarck', 'OH': 'Columbus', 'Ohio': 'Columbus', 'OK': 'Oklahoma City', 'Oklahoma': 'Oklahoma City', 
'OR': 'Salem', 'Oregon': 'Salem', 'PA': 'Harrisburg', 'Pennsylvania': 'Harrisburg', 'RI': 'Providence', 'Rhode Island': 'Providence', 
'SC': 'Columbia', 'South Carolina': 'Columbia', 'SD': 'Pierre', 'South Dakota': 'Pierre', 'TN': 'Nashville', 'Tennessee': 'Nashville', 
'TX': 'Austin', 'Texas': 'Austin', 'UT': 'Salt Lake City', 'Utah': 'Salt Lake City', 'VT': 'Montpelier', 'Vermont': 'Montpelier', 
'VA': 'Richmond', 'Virginia': 'Richmond', 'WA': 'Olympia', 'Washington': 'Olympia', 'WV': 'Charleston', 'West Virginia': 'Charleston', 
'WN': 'Madison', 'Wisconsin': 'Madison', 'WY': 'Cheyenne', 'Wyoming': 'Cheyenne'}
in this case you will need to change also my code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Like I said, I'm learning to.But is it not redundant to have to put each entry in twice. My snippet combines all three of his list into a dict. I get the not looping part though.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#8
This is to address the situation when you already have these three lists (States, Abb, Capitals) and want make suitable datastructure (as in buran's last example) for lookup:

data = dict()
for (state, short, capital) in zip(States, Abb, Capitals):
    data.update(dict.fromkeys((state, short), capital))
This will give dictionary where keys are state names and their abbreviations, value in both cases is state capital (on personal note - as non-american I learned a lot about US states capitals - I knew less than half of them) which enables fast lookup:

Output:
{'Alabama': 'Montgomery', 'AL': 'Montgomery', 'Alaska': 'Juneau', 'AS': 'Juneau', 'Arizona': 'Phoenix', 'AZ': 'Phoenix', 'Arkansas': 'Little Rock', 'AK': 'Little Rock', 'California': 'Sacramento', 'CA': 'Sacramento', 'Colorado': 'Denver', 'CO': 'Denver', 'Connecticut': 'Hartford', 'CT': 'Hartford', 'Delaware': 'Dover', 'DL': 'Dover', 'Florida': 'Tallahassee', 'FL': 'Tallahassee', 'Georgia': 'Atlanta', 'GA': 'Atlanta', 'Hawaii': 'Honolulu', 'HA': 'Honolulu', 'Idaho': 'Boise', 'IA': 'Boise', 'Illinois': 'Springfield', 'IL': 'Springfield', 'Indiana': 'Indianapolis', 'IN': 'Indianapolis', 'Iowa': 'Des Moines', 'IO': 'Des Moines', 'Kansas': ', Topeka', 'KS': ', Topeka', 'Kentucky': 'Frankfort', 'KY': 'Frankfort', 'Louisiana': 'Baton Rouge', 'LO': 'Baton Rouge', 'Maine': 'Augusta', 'MA': 'Augusta', 'Maryland': 'Annapolis', 'MD': 'Annapolis', 'Massachusetts': 'Boston', 'MC': 'Boston', 'Michigan': 'Lansing', 'MI': 'Lansing', 'Minnesota': 'Saint Paul', 'MN': 'Saint Paul', 'Mississippi': 'Jackson', 'MS': 'Jackson', 'Missouri': 'Jefferson City', 'MO': 'Jefferson City', 'Montana': 'Helena', 'MT': 'Helena', 'Nebraska': 'Lincoln', 'NB': 'Lincoln', 'Nevada': 'Carson City', 'NV': 'Carson City', 'New Hampshire': 'Concord', 'NH': 'Concord', 'New Jersey': 'Trenton', 'NJ': 'Trenton', 'New Mexico': 'Santa Fe', 'NM': 'Santa Fe', 'New York': 'Albany', 'NY': 'Albany', 'North Carolina': 'Raleigh', 'NC': 'Raleigh', 'North Dakota': 'Bismarck', 'ND': 'Bismarck', 'Ohio': 'Columbus', 'OH': 'Columbus', 'Oklahoma': 'Oklahoma City', 'OK': 'Oklahoma City', 'Oregon': 'Salem', 'OR': 'Salem', 'Pennsylvania': 'Harrisburg', 'PA': 'Harrisburg', 'Rhode Island': 'Providence', 'RI': 'Providence', 'South Carolina': 'Columbia', 'SC': 'Columbia', 'South Dakota': 'Pierre', 'SD': 'Pierre', 'Tennessee': 'Nashville', 'TN': 'Nashville', 'Texas': 'Austin', 'TX': 'Austin', 'Utah': 'Salt Lake City', 'UT': 'Salt Lake City', 'Vermont': 'Montpelier', 'VT': 'Montpelier', 'Virginia': 'Richmond', 'VA': 'Richmond', 'Washington': 'Olympia', 'WA': 'Olympia', 'West Virginia': 'Charleston', 'WV': 'Charleston', 'Wisconsin': 'Madison', 'WN': 'Madison', 'Wyoming': 'Cheyenne', 'WY': 'Cheyenne'}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
(Nov-06-2019, 01:40 PM)menator01 Wrote: But is it not redundant to have to put each entry in twice.
you either have to look up twice (if you want to accept both state and abbreviation) or have both state and abbreviation as keys in a single dict.
If you would loop, you can do it over zipped lists, so no need to create the dict. your code is essentially the same as
user_input = input("Type state or abrv> ")
for state, abb, capital in zip(States, Abb, Capitals):
    if user_input in (state, abb):
        print(f"Capital: {capital}, State: {state}, State Abbrviation: {abb}")
        break # just to quit earlier if match
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
I get what what is being said. Either loop or not loop. If there are say for example a million entries, it would take longer because you are having to loop through the entries to find the query? using a dict searches for the key and exits there? For every entry for a state, say like adding a motto that would need the state key pointing to that motto in the dict.
mottos = dict{'State_Name':'Some motto here','State_Abbr':'Some motto her'}
. Which is the better practice? Does it not have to loop using dicts as well? Does it not loop in the background. Something has to do a lookup for the key does it not? Just trying to learn. Sorry for all the questions.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020