14. Advanced Python Modules Flashcards
How do you import counter()
from collections import Counter
What does Counter() do?
Counter is a dict subclass which helps count hashable objects. Inside of it elements are stored as dictionary keys and the counts of the objects are stored as the value.
lst = [1,2,2,2,2,3,3,3,1,2,1,12,3,2,32,1,21,1,223,1]
- *Counter(lst)**
- *Counter({1: 6, 2: 6, 3: 4, 12: 1, 21: 1, 32: 1, 223: 1})**
How do you import defaultdict()?
from collections import defaultdict
What does defaultdict() do?
defaultdict is a dictionary-like object which provides all methods provided by a dictionary but takes a first argument (default_factory) as a default data type for the dictionary. Using defaultdict is faster than doing the same using dict.set_default method.
Does Defaultdict() ever create an error?
no
A defaultdict will never raise a KeyError. Any key that does not exist gets the value returned by the default factory.
What does defaultdict() look like?
d is empty but no error is created
d = defaultdict(object)
d[‘one’]
How do you import a namedtuple?
from collections import namedtuple
What does a namedtuple look like?
Dog = namedtuple(‘Dog’, [‘age’,’breed’,’name’])
sam = Dog(age=2,breed=’Lab’,name=’Sammy’)
frank = Dog(age=2,breed=’Shepard’,name=”Frankie”)
so:
sam
Dog(age=2, breed=’Lab’, name=’Sammy’)
sam.age
2
How do you create a file?
f = open(‘practice.txt’, ‘w+’)
f. write(‘test’)
f. close()
How do you get the current directory?
- *import os
os. getcwd()**
So, for example:
‘C:\Users\Marcial\Pierian-Data-Courses\Complete-Python-3-Bootcamp\12-Advanced Python Modules’
How do you list files in a directory?
import os
In your current directory
os.listdir()
How do you list files in a specific directory?
os.listdir(“C:\Users”)
How do you move a file?
import shutil
shutil.move(‘practice.txt’,’C:\Users\Marcial’)
How do you list files in a directory?
In your current directory
os.listdir()
How do you list files in a specific directory
os.listdir(“C:\Users”)
How do you delete a file?
import send2trash
send2trash.send2trash(‘practice.txt’)
How do you walk through a directory?
for folder , sub_folders , files in os.walk(“Example_Top_Level”):
For regular expressions, what does \d represent?
A digit
For regular expressions, what does \w represent?
Alphanumeric
For regular expressions, what does \s represent?
White space
For regular expressions, what does \D represent?
a non digit
For regular expressions, what does \W represent?
Non-alphanumeric
For regular expressions, what does \S represent?
non-whitespace
For regular expressions, what is a quantifier?
+ {n} * ?
where n is some number.
Now that we know the special character designations, we can use them along with quantifiers to define how many we expect.
Character Description Example Pattern Code Exammple Match
+ Occurs one or more times Version \w-\w+ Version A-b1_1
{3} Occurs exactly 3 times \D{3} abc
{2,4} Occurs 2 to 4 times \d{2,4} 123
{3,} Occurs 3 or more \w{3,} anycharacters
* Occurs zero or more times A*B*C* AAACC
? Once or none
For regular expressions, what does + represent?
Occurs one or more times
For regular expressions, what does * represent?
Occurs zero or more times.
For regular expressions, what does ? represent?
Once or none.
For regular expressions, what does {3} represent?
Occurs exactly 3 times.
For regular expressions, what does {2,4} represent?
Ocucurs 2 to 4 times.
For regular expressions, what does {3,} represent?
Occurs 3 or more times.