Python Flashcards
(280 cards)
In Python, data = [6, 8, 10, 12];
put data into an np array?
import np
MyArray = np.array(data)
In Python add dict1 to dict 2?
dict2.update(dict1)
In Python, create a dict that pairs keys and values? (keys and values are variables)
dict(zip(keys, values))
In Python and numpy:
arr_slice = arr[5:8]
arr_slice[1] = 12345
What is the result?
arr[6] equals 12345 (in the original object)
What is a python list comprehension to find all words in TEXT that are longer than 5 characters and appear at least 5 times?
[w for w in set(TEXT) if len(w) > 5 and FreqDist(TEXT)[w] >=5]
With NLTK, when tagging, what is a way to address the trade-off between accuracy and coverage?
Start with a tagger that is more accurate and back-off to a tagger that has greater coverage
In python, how can you reverse a dictionary and why do it?
How:
nltk.Index((value, key) for (key, value) in oldDict.items())
Why:
- Reverse lookup is faster
- dict() won’t deal with multiple values
Using NLTK and the brown corpus, how do you create a conditional frequency distribution based on genre?
ConditionalFreqDist((genre, word)
for genre in brown.categories()
for word in brown.words(categories = genre))
arr = np.array([1, 2, 3], [4, 5, 6])
What does arr[0,2] return?
The same as arr[0][2], which is 3.
arr = np.array([1, 2, 3], [4, 5, 6])
What does arr[0][2] return?
The same as arr[0,2], which is 3.
In iPython, 2 quick ways to time a function?
1) %timeit
2) %time
In Python, return an object’s True or False?
bool(object)
In Python, return the value of ‘a’ from myDict while deleting the key, value pair?
myDict.pop(‘a’)
In Python, load numpy?
import numpy as np
In iPython, look up magic words?
%.[TAB]
In Python’s interpreter, return list of attributes & methods of myObject?
myObject.[TAB]
In, iPython, what does this return?
‘numpy.load?’
All numpy functions that have load as part of the name.
In Python, s = ‘hello’
s[::-1]?
olleh
In Python, what does the following return if x = 30?
‘Yes’ if x > 10 else ‘No’
‘Yes’
In Python, s = ‘hello’
What does the following return?
For i, value in enumerate(s):
print(value)
h e l l o
In Python, return and remove the 3rd item in list T?
T.pop(2)
In Python, s = ‘hello’
What does the following return?
s[2:4]
ll
s[start:up to, but not including]
In Python and np, how do you flip an array Dat with rows and columns?
Dat.T()
In Python and np:
names = array of 7 names data = 7 by 4 d.array
What does the following return?
data[names == “Bob”|names == ‘Will’]
The rows of data at the index of names that equal ‘Bob’ or ‘Will’