python reference sheet

[list] -> [list]

Python
import random

def choose_random_points(points_list, num_points=3):
    return random.sample(points_list, num_points)
Python
def get_means_of_each_feature(xs: list[list[int]]):
    if len(xs) == 0:
        return None

    n_feature = len(xs[0])
    n = len(xs)

    means = []
    
    # option 1        
    for feature_idx in range(n_features):
        feature_sum = 0
        for i in range(n):
            feature_sum += xs[i][feature_idx]
        means.append(feature_sum / n)

    # option 2 
    def get_mean(xs):
        return sum(xs) / len(xs)
        
    for feature_idx in range(n_features):
        feature_mean = get_mean([x[feature_idx] for x in xs])
        means.append(features_mean)
        
    return means

[list] -> num

Python
def argmin(xs: list[int]):
    assert len(xs) > 0
    
    min_found = float('inf')
    min_index = None
    for i, x in enumerate(xs):
        if x < min_found:
            min_found = x
            min_index = i
    return min_index
Python
def distance(p1: list[int], p2: list[int]):
    summed_sq_diff = 0
    for v1, v2 in zip(p1, p2):
        summed_sq_diff += (v1-v2) ** 2

    return summed_sq_diff ** 0.5

# spelled out    
def distance(p1: list[int], p2: list[int]):
    def sq_diff(x, y): 
        return (x-y)**2
        
    # list of squared differences
    list_sq_diff = [sq_diff(v1,v2) for v1,v2 in zip(p1,ps)]    
    # sum
    summed_sq_diff = sum(list_sq_diff)    
    # sq root
    return summed_sq_diff ** 0.5