Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Phyton code labs Dictionary and Set With more tools and skills, less work is required. With a list of lists, you can create pairs of

Phyton code labs

image text in transcribed
Dictionary and Set With more tools and skills, less work is required. With a list of lists, you can create pairs of abbreviations with their meaning. To decipher an abbreviation, you can look up the first item to find the second, like this: abbrev [ ['NRG', 'Northridge"], ['AUS', 'Austin'], ['TX', 'Texas'], ['US', 'United States"] ] target=input('Enter the abbreviation, I'll tell you what it stands for: ") for pair in abbrevi if pair[0]==target: print(pair[1]) This works, but there is a better way. It is easier to use dictionaries, like this: abbrev {'NRG': Northridge', "AUS": "Austin', TX': "Texas', 'US':United States"} target=input('Enter the abbreviation, I'll tell you what it stands for: ") print(abbrev.get(target)) # or print(abbrev target]) Consider another situation. Say you need a collection of one of each (unique) letter used in a sentence: sentence="this is a fine day to go to the park and have a picnic with lemons and zebras' letters=[ ] for letter in sentence: if not letter in letters: letters+=letter print(letters) This works, but it is easier to create a set: sentence='this is a fine day to go to the park and have a picnic with lemons and zebras' letters=set(sentence) print(letters) Different objects (containers) have different advantages and disadvantages. Many originated in mathematics (sets and set theory) and are used in programming languages. By using dictionary and set, instead of list, you can write smaller, faster, better code. Pickling Here is another powerful tool: pickle. Say you want to save the list (or dictionary) abbrev and the string sentence to a file; and then later, read that data back. How would you do this? To save these objects to a text file and then restore is awkward. Saving many different objects: list, dictionary, set, string, etc, to a text file and reloading them is awkward, because every item must be converted to and from a string. Using pickle to "dump" and "load" objects, it's easy! This lab has three parts. The first two parts try out dictionaries and sets. You don't have to learn every operation these provide; but you should learn they can be better than list, tuple and string. The third part is to try out pickle operations. Part 1: (10 points) Write a program that uses a dictionary. The dictionary will operate like the abbrev dictionary above. Each key is a two-letter abbreviation for a state; each value is the full state name. Create a dictionary; fill it with data from the same file used in lab 6

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Programming questions

Question

=+ List three determinants of the terms of trade.

Answered: 1 week ago