Python Cook Book: Chapter 1 Data Structures & Algorithms

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

what does a chainmap do?

It simply keeps a list of the underlying mappings and redefines common dictionary operations to scan the list. If there are duplicate keys, the values from the first mapping get used. Also operations that mutate the mapping always affect the first mapping.

What does the counter object take in as a parameter?

It takes in any sequence of hash able input items.

What is a priority Queue?

It is a queue that sorts items by a given priority and it always returns the item with the highest priority on each pop operation.

How does the groupby() function works?

It works by scanniung a sequence and finding sequential "runs" of identical values. (Or values returned by the given key function).

why is deque better than a dictionary?

It is better because a deque allows us to remove and add elements onto both sides of it. unlike a dictionary. Also, a dictionary is restructure every time.

What needs to be known when using the zip() function?

It is important to know that zip() creates an iterator that can only be consumed once.

the itemgetter() functionality is similar to what other method?

It is similar to a lambda expression.

How to choose between sets and lists when trying to map keys to multiple values?

The choice of whether or not to use lists or sets depends on intended use. Use a list if you want to preserve the insertion order of the items. Use a set if you want to eliminate duplicates (and don't care about the order).

what is the difference between list comprehensions and generator expressions?

The difference is that the list comprehensions and generator expressions is their syntax. With a list comprehensions you can iterate through the items as many times as you want and with a generator you can only do that once.

what is the difference between postional arguments and keyword arguments?

positional arguments are arguments that take in while keyword arguments are stored into a dictionary like form.

Why is deque better than a list?

Because with a deque we can append and delete items from either end. Also a deque runs much smoother than a list.

What is a counter like?

A counter is like a dictionary that maps the items to the number of occurrences.

What is a good feature of the counter instances?

A good feature tis that they can be easily combined using various mathematical operations. Such operations include addition and subtraction.

Why is a default dictionary better than a dictionary?

Because a default dictionary automatically initializes the first value so that i can simply focus on adding items.

Why is it better to create a function that eliminates duplicates from a sequence?

Because that way we can maintain order of the sequence.

Why is an ordered dictionary useful?

It can be useful when you want to build a mapping that you may want to later serialize or encode into a different format.

what does the compress callable class from the itertools module do?

It compresses a boolean list and another iterable. so that it can output an iterable that is the result of the mapping of true values from the boolean list to the iterable.

What is a dictionary?

It is a mapping where each key is mapped to a single value.

What is star unpacking?

Start unpacking is using the * before a variable that you want to assign an arbitrary length of values.

how would one go about sorting dictionaries by keys?

We can use the itemgetter class from the operators module in order to get the keys that we are looking for.

How to determine the most frequently occurring items in a sequence?

We use the collections module and from it import the counter class. We then use the counter class' method most_common() and as a parameter, we give it the 2 or 3 most common items.

How to unpack a sequence and throw away values that we dont want?

We use the start unpacking with the "_" like such: name, *_, (*_, year) = data

How to unpack a sequence of arbitrary length?

You can unpack a sequence of arbitrary length by using the * keyword. the * allows us to assign as many variables onto a single variable.

why is recursion not a strong feature of python?

because of python's inherent recursion limit.

what does the following statement do? import heapq heapq.nlargest(3, nums) heapq.nsmallest(3,nums)

for the nlargest function it finds the 3 largest elements of the sequence nums. for the nsmallest function if finds the 3 smallest elements of the sequenece nums.

What is a nametuple?

collections.namedtuple() is actually a factory method that returns a subclass of the standard Python tuple type. You feed it a type name, and the fields it should have, and it returns a class that you can instantiate, passing in values for the fields you've defined, and so on

What is an ordered dictionary?

internally maintains a doubly-linked list that orders the keys according to insertion order. An ordered dictionary is double the size of a normal dictionary.

what does the following statement do? head, *tail = data

it assigns the first value of data to head and the proceeding values are assign to tail.

What does the following lines of code do? from collection import deque q = deque(maxlen=9)

it imports deque from the collections module. It then assigns a deque with a max length of 9 to the variable q.

what does the the following statement do? import heapq heap = num //num is a list of numbers heapq.heapify(heap) heapq.heappop(heap)

it imports the heapq module. it then assigns the num list onto the heap variable. The heap variable is then turn into a heapq object. Once it is a heapq object its smallest value is set at the 0 index. everytime the heappop() method is call it pops the next smallest integer.

what does the heapq module do?

it is the implementation of the priority queue algorithm.

What does the following statements do? a = slice(10, 50, 2) s = 'HelloWorld' a.indeces(len(s)) for i in range(*a.indices(len(s))): print(s[i])

it iterates through the strings and it prints out the string literals that start at index 5 in steps of 2 and stops iterating at index 10.

construct a dictionary comprehension.

p1 = {keys: value for key, value in prices.items() if value > 200}

what is another way of writing a dictionary comprehension?

tech_names = {"AAPL", "IBM", "HPQ", "MSFT"} p2 = { key:prices[key] for key in prices.keys() & tech_names } However this solution is much slower then the other solution on 32.

What is one advantage of the namedtuple class?

the fact that it is interchangable with a tuple and supports all of the usual tuple operations such as indexing and unpacking.

what are the differences between these 2 expressions? min_shares = min(s["shares"] for s in portfolio) min_shares = min(portfolio, key=lambda s: s["shares"])

the first expression uses a generator within the min function. and the second one uses a lambda function. The generator is much better then the lambda expression because it is faster.

what type of object is the following and what does it do? e = [x if x > 50 else 0 for x in range(0,101)]

this is a comprehension list object and it creates a list with values from range 0 to 100. Since there is a conditional statement in the comprehension list this means that for half of the list the values will be 0 since they did not meet the conditional given.

How to find common keys and values in a dictionary?

you can find common elements in dictionaries by using logic operators.

How to turn a dictionary into a sequence?

zip(dictionary.values(), dictionary.keys()) allows us to iterate throuth the dictionary like a sequence.


Kaugnay na mga set ng pag-aaral

MCN 374 Exam 1 - Ch 1, 2, 4-9, 11, 12, 15, 20

View Set

Immunology: Humoral Immune Responses

View Set

FIN 310 Exam 3 Clicker Questions

View Set

Chapter 15: Adrenergic Agonists and Antagonists

View Set

Chapter 5 - Data and Process Modeling

View Set

Academic Team - Nobel Prize Winners (Physics)

View Set

Chapter 7.3 Energy Changes in Reactions

View Set