copy_of_lab03_spotify

.py

School

University of Michigan *

*We aren’t endorsed by this school

Course

206

Subject

Computer Science

Date

Feb 20, 2024

Type

py

Pages

10

Uploaded by erzas088

Report
# -*- coding: utf-8 -*- """Copy of lab03_spotify.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/18ur4iO68n8VS8z0yY53_BiUUb7pjeig6 # Unordered Collections, Tuples and Spotify Songs ## Unordered Collections We saw *ordered* collections when we introduced strings and lists. """ string = "this is a collection of characters" string[8:20] ## lists can store aribitrary kinds of data lst = [True, ["a", "nested", "list"], 12345] len(lst) """**Unordered** collections are those where we don't use order to find items, but use some other method of retrieving values. ### Sets The first unordered collection we introduce is a set. """ dord = {"Department", "of", "Redundancy", "Department"} dord len(dord) """Only once copy of each value can be included in a set. It is useful for creating collections that include only unique elements. Create a set that has uses the three pieces of a phone number for these two numbers: (i.e., 734 is a value that should appear in the set) * 734-123-4568 * 734-613-2640 """ {734, 123, 4568, 613, 2640} """<details> ``` {734, 123, 4568, 734, 613, 2640} ``` </details> We often want to compare sets and see where they are the same and where they are different """
states1 = {"MI", "AZ", "FL", "DE", "OR"} states2 = {"FL", "MI", "MN", "AZ", "AK"} states1.intersection(states2) """Or where they differ:""" states1.difference(states2) states2.difference(states1) states1.symmetric_difference(states2) """What course might Alice recommend to Bob? (Do it with Python!)""" alice = {"Stats 206", "Stats 306", "Econ 101", "EECS 183"} bob = {"EECS 183", "Stats 206", "Math 241" ,"Econ 101"} alice.difference(bob) """<details> ``` alice.difference(bob) ``` </details> We've seen the use of `+` to join ordered collections before. For sets we use `|`. Between Bob and Alice, how many unique classes have they take in total? """ classes = bob | alice len(classes) """<details> ``` len(alice | bob) ``` </details> ### Dictionaries Dictionaries connect *keys* and *values*. For dictionaries, all keys must be distinct, but the values can be duplicated. We specify them like this: """ number_of_legs = {"dog": 4, "human": 2, "centipede": 100, "slug": 0, "cow": 4} """or""" number_of_legs = { "dog": 4, "human": 2,
"centipede": 100, "slug": 0, "cow": 4 } """As with ordered collections, we retrieve using square brackets `[]`.""" number_of_legs["centipede"] """Dictonaries are "mutable" or "changeable", meaning we can add values.""" number_of_legs["pirate"] = 1 number_of_legs """Add a new entry to the `number_of_legs` dictionary for ants. Use a comparison to prove that ants have more legs than cows.""" number_of_legs["ants"] = 6 number_of_legs["ants"] > number_of_legs["cow"] """<details> ``` number_of_legs["ant"] = 6 number_of_legs["ant"] > number_of_legs["cow"] ``` </details> Occasionally it is helpful to get the *set* of keys from a dictionary with the `.keys()` method. Show the set of things for which we have the number of legs. """ number_of_legs.keys() """<details> ``` number_of_legs.keys() ``` </details> Likewise, as you probably guessed, we can get the values with `.values()`. Output just the values, without the keys. Then call the `set()` function on the result to show the *unique* set of leg values. """ print(number_of_legs.values()) set(number_of_legs.values()) """<details> ``` print(number_of_legs.values())
set(number_of_legs.values()) ``` </details> ### Tuples Tuples are fixed length lists in Python. Typically, they are small (2 to 5 items). They use round braces to denote: """ (3, 2, 1) """It's not uncommon to encounter a function that will return a tuple, where each item in the tuple has a given value.""" def first_last(w): return (w[0], w[-1]) first_last("hello") """This is convenient because we can easily assign items in a tuple to variable names using *multiple assignment*: """ a, b = first_last("elephant") print(a) print(b) """Write a function that finds the minimum and maximum value of a collection as a tuple. Find the difference of these two values for this collection: """ nums = [9, -1, 105, 44, -23, 2001, 4] def min_and_max(lst): lst.sort() return (lst[0], lst[-1]) print(min_and_max(nums)) """<details> ``` def minmax(lst): return (min(lst), max(lst)) mn, mx = minmax(nums) mx - mn ``` </details> ### Dictionaries of Lists For many data analysis problems, we would wish to study a **population**, but
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help