6 | Dictionaries and Sets

¡Supera tus tareas y exámenes ahora con Quizwiz!

proper

(Fill-In) A subset is a(n) _________ subset of another set if all the subset's elements are in the other set and the other set has more elements.

animation frame

(Fill-In) A(n) _______________________ specifies everything that should change during one plot update. Stringing together many of these over time creates the animation effect.

the law of large numbers

(Fill-In) As we toss a coin an increasing number of times, we expect the percentages of heads and tails to become closer to 50% each. This is a manifestation of ____________________________________.

items

(Fill-In) Dictionary method _______ returns each key-value pair as a tuple.

keys

(Fill-In) Dictionary method __________returns an unordered list of the dictionary's keys.

fargs

(Fill-In) FuncAnimation's _______ keyword argument enables you to pass custom arguments to the function that's called once per animation frame.

split

(Fill-In) String method _______ tokenizes a string using the delimiter provided in the method's string argument.

animation, FuncAnimation

(Fill-In) The Matplotlib module's ______________________________ function dynamically updates a visualization.

disjoint

(Fill-In) Two sets are ____________ if the sets do not have any common elements.

It updates the value associated with the key, replacing the original value.

(Fill-In) What does an expression of the following form do when the key is in the dictionary? dictionaryName[key] = value

set

(Fill-In) You can create a set from another collection of values by using the built-in ______ function.

dictionaries

(Fill-In) _________________ can be thought of as unordered collections in which each value is accessed through its corresponding key.

In [1]: text = 'to be or not to be that is the question' In [2]: unique_words = set(text.split()) In [3]: for word in sorted(unique_words): ...: print(word, end=' ') ...: be is not or question that the to

(IPython Session) Assign the following string to variable text, then split it into tokens with string method split and create a set from the results. Display the unique words in sorted order. 'to be or not to be that is the question'

In [1]: states = {'VT': 'Vermont', 'NH': 'New Hampshire', 'MA': 'Massachusetts'} ...: In [2]: states Out[2]: {'VT': 'Vermont', 'NH': 'New Hampshire', 'MA': 'Massachusetts'}

(IPython Session) Create a dictionary named states that maps three state abbreviations to their state names, then display the dictionary.

In [1]: roman_numerals = {'I': 1, 'II': 2, 'III': 3, 'V': 5} In [2]: list(roman_numerals.keys()) Out[2]: ['I', 'II', 'III', 'V'] In [3]: list(roman_numerals.values()) Out[3]: [1, 2, 3, 5] In [4]: list(roman_numerals.items()) Out[4]: [('I', 1), ('II', 2), ('III', 3), ('V', 5)]

(IPython Session) For the following dictionary, create lists of its keys, values and items and show those lists. roman_numerals = {'I': 1, 'II': 2, 'III': 3, 'V': 5}

In [1]: {10, 20, 30} - {5, 10, 15, 20} Out[1]: {30} In [2]: {10, 20, 30} ^ {5, 10, 15, 20} Out[2]: {5, 15, 30} In [3]: {10, 20, 30} | {5, 10, 15, 20} Out[3]: {5, 10, 15, 20, 30} In [4]: {10, 20, 30} & {5, 10, 15, 20} Out[4]: {10, 20}

(IPython Session) Given the sets {10, 20, 30} and {5, 10, 15, 20}, use the mathematical set operators to produce the following sets: 1. {30} 2. {5, 15, 30} 3. {5, 10, 15, 20, 30} 4. {10, 20}

In [1]: roman_numerals = {'I': 1, 'II': 2, 'III': 3, 'V': 5, 'X': 100} In [2]: roman_numerals['x'] = 10 In [3]: roman_numerals Out[3]: {'I': 1, 'II': 2, 'III': 3, 'V': 5, 'X': 100, 'x': 10}

(IPython Session) String dictionary keys are case sensitive. Confirm this by using the following dictionary and assigning 10 to the key 'x'—doing so adds a new key-value pair rather than correcting the value for the key 'X':

In [1]: import random In [2]: numbers = [random.randrange(1, 6) for i in range(50)] In [3]: from collections import Counter In [4]: counter = Counter(numbers) In [5]: for value, count in sorted(counter.items()): ...: print(f'{value:<4}{count}') ...: 1 9 2 6 3 13 4 10 5 12

(IPython Session) Use a comprehension to create a list of 50 random integers in the range 1-5. Summarize them with a Counter. Display the results in two-column format.

In [1]: {number: number ** 3 for number in range(1, 6)} Out[1]: {1: 1, 2: 8, 3: 27, 4: 64, 5: 125}

(IPython Session) Use a dictionary comprehension to create a dictionary of the numbers 1-5 mapped to their cubes:

In [1]: set('abc def ghi jkl mno').issuperset('hi mom') Out[1]: True

(IPython Session) Use sets and issuperset to determine whether the characters of the string 'abc def ghi jkl mno' are a superset of the characters in the string 'hi mom'.

False. A view does not have its own copy of the corresponding data from the dictionary. As the dictionary changes, each view updates dynamically.

(True/False) A view has its own copy of the corresponding data from the dictionary.

False. Assigning to a nonexistent key inserts a new key-value pair. This may be what you intend, or it could be a logic error if you incorrectly specify the key.

(True/False) Assigning to a nonexistent dictionary key causes an exception.

False. Dictionary keys must be unique. However, multiple keys may have the same value.

(True/False) Dictionaries may contain duplicate keys.

False. Generally, displaying more frames-per-second yields smoother animation.

(True/False) Generally, displaying fewer frames-per-second yields smoother animation.

False. Set method pop returns an arbitrary set element.

(True/False) Set method pop returns the first element added to the set.

False. Sets are collections of unique immutable objects.

(True/False) Sets are collections of unique mutable and immutable objects.

False. All the comparison operators may be used to compare sets.

(True/False) Sets may be compared with only the == and != comparison operators.

False. The == comparison evaluates to True if both dictionaries have the same key-value pairs, regardless of their order.

(True/False) The == comparison evaluates to True only if both dictionaries have the same key-value pairs in the same order.

False. The actual number of frames-per-second also can be affected by the amount of work performed in each frame and the speed of your computer's processor.

(True/False) The actual number of frames-per-second is affected only by the millisecond interval between animation frames.

update

Set method ____________ performs a union operation, modifying the set on which it's called.


Conjuntos de estudio relacionados

Quiz Unit 9 - Government Sponsored and Other Financing

View Set

Bibl 1200 His of Is Owens Final Exam

View Set

Marine Science B (Unit 1- Unit 5)

View Set

What is the longest word you know

View Set