Python I Flashcards
attribute for getting the dimensions of a numpy array (e. g. (3, 4, 2))
arr.shape
attribute for getting the datatype of a numpy array
arr.dtype
attribute for getting the number of dimensions of a numpy array
arr.ndim
attribute for getting the number of all elements of a numpy array
arr.size
creating an array in numpy
np.array(my_list)
creating a range in numpy
np.arange(start, stop, step)
creating ranges of values in numpy
np.linspace(start, stop, numberOfElems, endpoint=True/False)
get multiple elements of a numpy array (index)
one_dim[[3, 4, 6, 15]] (gets elements at index 3, 4, 5, 6 and 15)
get the element of the 3rd row and 4th column of a numpy array
arr[2, 3] or arr[(2, 3)]
get the second row of a multidimensional numpy array
arr[1]
get the second column of a multidimensional numpy array
arr[: , 1]
extract the bottom right 2x2 corner of a 3x4 numpy array
arr[1:3, 2:4]
indexing of numpy arrays is also possible via masks
mask = np.array([[True, False, False, True],
[False, True, False, True],
[False, False, True, True]])
print(“two_dim[mask]”)
print(two_dim[mask])
(Returns flat (1D) array of elements where mask = True)
only extract even number out of a numpy array
mask = (arr % 2) == 0
print(arr[mask])
additional information numpy arrays
x2 = two_dim[:][2]
This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”
additional information numpy arrays
x2 = two_dim[:][2]
This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”
additional information numpy arrays
x2 = two_dim[:][2]
This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”
additional information numpy arrays
x2 = two_dim[:][2]
This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”
set all elements from index 2 to 4 to zero in a numpy array
one_dim[2:5] = [0, 0, 0]
or
one_dim[2:5] = 0 (broadcasting)
set all entries of a twodimensional numpy array to 0
two_dim[:] = 0
set the second to third element of the second row of a numpy array to 0
two_dim[1, 1:4] = 0
(also subarrays can be broadcasted, e. g.
two_dim[:, 1:3] = [7, 8])
When can numpy arrays be reshaped?
When the number of elements stays the same.
a = np.arange(24)
a = a.reshape(2, 3, 4)
a = a.reshape(4, 6)
a = a.reshape(6, -1)
add an empty dimension
a1 = a[:, None, :]
a2 = a[:, np.newaxis, :]
Other important numpy functions
print(np.append(a, np.array([1, 2, 3])))
print(np.concatenate([a, a, a]))
print(np.repeat(a, repeats=5))
print(np.tile(a, reps=5))
important: sum in numpy arrays
Sum (similar functions available for: mean, standard deviation, etc.)
arr = np.arange(2 * 3 * 4).reshape((2, 3, 4)) * 3
print(arr.sum(axis=0)) # Sum along first axis/dimension (axis 0 will be “collapsed” -> shape: (3, 4))
print(arr.sum(axis=1)) # Sum along second axis/dimension (axis 1 will be “collapsed” -> shape: (2, 4))
print(arr.sum(axis=2)) # Sum along second axis/dimension (axis 2 will be “collapsed” -> shape: (2, 3))
print(arr.sum(axis=(1, 2))) # Sum along second + third axis (axis 1 + 2 will be “collapsed” -> shape: (2,))
print(arr.sum()) # “Global” sum along all axes/dimensions
Get the (flat!) index of the minimum of a numpy array and transform it into an array-shaped index
i_min_flat = arr.argmin()
i_min_shape = np.unravel_index(i_min_flat, shape=arr.shape)
numpy functions with conditionals
print(np.where(arr % 2 == 0, arr, other)) # If condition is True, take from “arr”, else take from “other”
print((arr > 50).all()) # Check if all elements meet the condition
print((arr > 50).any()) # Check if at least one element meets the condition
random values in numpy
rng = np.random.default_rng(seed=1234)
arr = rng.random((4, 3))
arr = rng.integers(low=1, high=25, size=5)
arr = rng.choice(np.arange(100), size=10, replace=False)
sort a numpy array
np.sort(arr)
or
arr.sort() - in-place
important info for numpy slicing and reshaping
Reshaping and slicing does not copy the original array, it only creates another “view” on the data.
regex - search for the first occurence of a pattern
re.search(pattern, text)
regex - find a pattern at the beginning of the string
re.match(pattern, text)
regex - groups
Groups can be used to only search for subpatterns within a search pattern (e. g. “H(all)o, how (are) you?”)
The found patterns can be accessed like this (0 is the whole pattern, 1 is the first group)
match_object.group(1)
Getting additional information from regex group match objects
match_object.start(1)
match_object.end(1)
match_object.span(1)
regex - find all occurences of a pattern
re.findall(pattern, text) -> list
(re.finditer does the same, but does it one item at a time and returns a match object)
regex - a set of characters to match
[…]
e. g. “[cbr]at”
regex - specify ranges
[…]
e. g. “[0-5]”
regex - negate patterns
e. g. “[^0-9]”
regex - predefined group for digits
\d
regex - predefined group for non-digits
\D
regex - predefined group for whitespace characters
\s
regex - predefined group for non-whitespace characters
\S
regex - predefined group for word characters
\w
regex - predefined group for non-word characters
\W
regex - match any character (except newline)
.
e. g. “.m.”
regex - search for alternative patterns
|
e. g. “[bcr]at|dog”
regex - match any number of repetitions (also 0)
*
e. g. “([bcr]at|dog)*”
regex - match at least one repetition
+
e. g. “([bcr]at|dog)+”
regex - be not- greedy (not search for as many repetitions as possible)
? (suffix)
regex - positive lookahead
only match if the following is XYZ
”(?=XYZ)”
regex - negative lookahead
only match if it is not followed by XYZ
”(?!XYZ)”
regex - positive lookbehind
only match if the preceeding is XYZ
”(?<=XYZ)”
regex - negative lookbehind
only match if the preceeding is not XYZ
”(?<!XYZ)”
call a program with the subprocess module
p = subprocess.Popen([“some_program”, “argument1”, “argument2”]) - will not wait for the program to finish
p.wait(timeout=2) - will wait 2 seconds for the program to finish (except subprocess.TimeoutExpired)
get the output and errors of a program
p = subprocess.Popen([“ping”, “www.jku.at”], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
outs, errs = p.communicate(timeout=15)
simple way of calling a program
result = subprocess.run([“some_program”, “argument1”, “argument2”])
result.returncode
result.stdout
result.stderr
only execute code in the main process
if __name__ == “__main__”
use multiprocessing
from multiprocessing import Pool
arguments = list(range(100))
without dynamic iterable:
with Pool(4) as p:
pool_returns = p.map(fcn, arguments)
with dynamic iterable:
with Pool(4) as p:
pool_returns = p.imap(fcn, arguments)
(or imap_unordered)
for multiple arguments, starmap must be used
add a command line argument
parser = argparse.ArgumentParser()
parser.add_argument(“-n3”, “–int_number3”, type=int, required=True, help=”required int”)
add a command line argument which accepts mutiple inputs
parser.add_argument(“–numbers”, nargs=3, type=int)
parser.add_argument(“–items”, nargs=”+”)
parser.add_argument(“–items”, nargs=”*”)
access command line arguments
args = parser.parse_args()
my_filename = args.filename
fall back to the default implementation of “==” if the if-statement returns False
def __eq__(self, other):
if isinstance(other, Point):
return self.x == other.x and self.y == other.y
return NotImplemented
override output of the line “p1”
__repr__(self)
override output of the line “print(p1)”
__str__(self)
override indexing
__getitem__(self, key)
override “+”
__add__(self, other)
get the type of an object
type(dog1).__name__
check if a variable is either an integer of a floating point number
isinstance(num, (int, float))
syntax of a static method
@classmethod
def some_class_method(cls):
print(f”this is a class method: {cls}”)
read file content
with open(“my_module.py”, “r”) as f:
print(f.read())
with open(“my_module.py”) as f:
for line in f:
print(line, end=””)
with open(“my_module.py”) as f:
while chars := f.read(10):
print(“Chunk:”)
print(chars)
write file content
with open(“some_file.txt”, “w”) as f:
f.write(“This overwrites an existing file\nor creates a new one with this text!\n”)
with open(“some_file.txt”, “a”) as f:
f.write(“And here this line is appended.\n”)
with open(“some_file.txt”, “a”) as f:
print(“And another line via print()!”, file=f)
get current working directory
os.getcwd()
create a directory
os.makedirs(“new_directory”, exist_ok=True)
rename files or directories
os.rename(“new_directory”, “my_new_new_directory”)
remove a directory
os.removedirs(“my_new_new_directory”)
remove a file
os.remove(“some_file.txt”)
list all contents of a directory
os.listdir()
os.path methods
path = os.path.join(“some”, “directory”, “filename.py”)
head, tail = os.path.split(path)
os.path.dirname(path)
os.path.basename(path)
root, ext = os.path.splitext(path) - extensions of a file
os.path.exists(path)
check if a path is a file
os.path.isfile(path))
check if a path is a directory
os.path.isdir(“.”) - “.” refers to the current working directory
os.path methods
path = os.path.join(“some”, “directory”, “filename.py”)
head, tail = os.path.split(path)
os.path.dirname(path)
os.path.basename(path)
root, ext = os.path.splitext(path) - extensions of a file
os.path.exists(path)
os.path.abspath(path)
list of all files and subdirectories in the current working directory
glob.glob(“*”)
search the current working directory for python files
glob.glob(“*.py”)
search for files and folders recursively
glob.glob(“**”, recursive=True)
search in a directory and all subdirectories for python files
glob.glob(os.path.join(“some_folder”, “**”, “*.py”), recursive=True)
pickle
import dill as pickle
always use with(“path”, “mode”) as f
pickle.dump(my_objects, f)
data = pickle.load(f)
import modules
import sys
import sys as system
from os import path
import os, sys
from os import path, makedirs
from my_module import add as my_add
create a list
empty_list = []
empty_list = list()
a_list = [1, 2.3456, 3, “abc”]
navigating through nested lists
nested_list_element = nested_list[2][3][0]
check if an element is contained in a list
“abc” in a_list
sort lists
numeric_list.sort() - in-place
sorted_list = sorted(numeric_list) - numeric_list is not changed
create a tuple
empty_tuple = ()
empty_tuple = tuple()
single_element_tuple = (1,)
a_tuple = 1, “2”, 3
a_tuple = (1, “2”, 3)
create a set
empty_set = set()
my_set = {1, 2, 3, 3}
add/remove elements to a set
my_set.add(4)
my_set.remove(2)
common operations on sets
Union
new_set = my_set | {1, 4, 7}
new_set = my_set.union({1, 4, 7})
Intersection
new_set = my_set & {1, 4, 7}
new_set = my_set.intersection({1, 4, 7})
Difference
new_set = my_set - {1, 4, 7}
new_set = my_set.difference({1, 4, 7})
create a dictionary
empty_dict = {}
empty_dict = dict()
some_key = “abc”
dictionary = dict(some_key=3.24, other_key=”twh”)
dictionary2 = {“stringkey”: 55, some_key: “someitem”, 23: 4}
get all keys or values of a dictionary
dictionary.keys()
dictionary.values()
dictionary.items() - tuples
additional dictionary stuff
Remove key and return its associated value
removed = dictionary2.pop(“stringkey”)
Get a value and use a default value if key does not exist
value = dictionary.get(“nonexistingkey”, “defaultvalue”)
dictionary iteration
for key in dictionary: (Equal to: dictionary.keys())
print(key)
for value in dictionary.values():
print(value)
for key_value in dictionary.items():
print(key_value)
for key, value in dictionary.items():
print(key, value)
list comprehensions
old_list = [1, 2, 3, 4, 5]
new_list = [str(i) for i in old_list]
new_list = [str(i) for i in old_list if i < 3]
new_list = [str(i) if i < 5 else “end” for i in old_list]
reverse a list
reversed_list = some_list[::-1]
unpacking
a, b, c, d = [1, 2, 3, 4]
a, b, *rest = [1, 2, 3, 4]
a, *rest, d = [1, 2, 3, 4]
for loop with index
for i, elem in enumerate(a_tuple):
print(i, elem)
iterate through a string with index
for i, char in enumerate(“hello”):
print(f”loop iteration: {i}, element: ‘{char}’”)
integer division
//
floor value of a division
string functions
uppercase_string = conc_strings.upper()
lowercase_string = “SILENCE!”.lower()
number_of_bla = repeated_string.count(“bla”)
separated_elements = “my-string-elements”.split(“-“)
joined_elements = “;”.join(separated_elements)
replacement = “This is a placeholder”.replace(“placeholder”, “string”)
check_for_substring = “string” in conc_strings
locate_substring = replacement.find(“is a”)
formatted strings
formatted_string = “You can format floating point values as :width.precision “ \
f”{long_number:10.5f}”
another_formatted_string = f”{long_number:.{n_decimals}f}”
console
print(“Hello, world!”)
my_input = input(“Please enter something: “)