Find a substring [closed]

2024/11/17 21:55:53

You are given a dictionary of the US states and their capitals. The keys in the dictionary are states and the values are capital names.

Write a code to return a list of all capitals that contain the name of a state in their name as a substring.

HINT: For example, Indiana as a capital name and Indianapolis as a state name is one of the key/value pairs that your code would find. Your code should add Indianapolis to the list. After you found all capitals and added them to the list, print out the list

Run this cell to create a dictionary of states' capitals

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','Illinios': 'Springfield','Indiana': 'Indianapolis','Iowa': 'Des Monies','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','Neveda': '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','Rhoda 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'  
}

Code:

result = []
for x in capitals.keys():if(x in capitals[x]):result.append(capitals[x])
print(result)
Answer

Oh, I got it. I missunderstood your question. This will be work.

print([v for k, v in capitals.items() if k in v])# more readable
print([capital for state, capital in capitals.items() if state in capital])

This will return

['Indianapolis', 'Oklahoma City']
https://en.xdnf.cn/q/118759.html

Related Q&A

Image processing with single to multiple images

I have an Image showing below: I need to crop the order using python coding. What I need is only the card. So I want to crop the border. How to do it??This is the output I got using the code mentione…

SQLAlchemy Automap not loading table

I am using SQLAlchemy version 2.0.19 (latest public release). I am trying to map existing tables as documented in https://docs.sqlalchemy.org/en/20/orm/extensions/automap.html#basic-use I created a SQL…

Create a base 12 calculator with different limits at diferent digits with python

I want o create a calculator that can add (and multiply, divide, etc) numbers in base 12 and with different limits at the different digits.Base 12 sequence: [0,1,2,3,4,5,6,7,8,9,"A","B&q…

Python Program to check if a number is armstrong or not is not working, what am I doing wrong?

n=int(input("Enter a Number: ")) x=0 y=0 z=0while(n>0):x=n%10y=x**3z=z+yn=n//10print (z) #The z here is the same value which I enter, yet it doesnt work. #If I enter 407 as n, z becomes (4…

Python(Scrapy) unpredictable mistake with import load_entry_point

I have such problem, I did nothing with Python or Scrapy, but when I started today my computer I got such error. I have found many different posts and tried some tips and advices, unfortunately, they a…

Debugging RadioButtons program in Python

from Tkinter import *class Application (Frame):def __init__(self, master):Frame.__init__(self, master)self.grid()self.create_widgets()def create_widgets(self):Label(self, text = "Select the last b…

Why am I getting an Internal Server error

My python script runs just fine on the Apache server locally set up on my computer, however, on importing the json2html library I am getting an internal server errorThe moment I comment the import stat…

Save pixel data in txt format in PIL

My program is to extract the pixel from an image and to save the pixel data in the text file for analysis. My picture is a binary image that gives only 255 and 0 sHere is the program:from PIL import Im…

ValueError: view limit minimum 0.0 is less than 1 and is an invalid Matplotlib date value

Ive been given the python script where matplotlib is used , when running the script it opens the window and display graph. its working perfectly on my laptop. But this error occurs when I upload the fi…

Python win32com Outlook Stores not iterable

Trying to list all Outllook stores (and finally all e-mails in those stores):import win32com.clientoutlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") sto…