Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

One a Day One Liners with Python — Week 3

Jeremy Brown
Python in Plain English
5 min readJan 15, 2023

--

Photo by Vlado Paunovic on Unsplash

Jan 21, 2023

from math import sqrt

a = [
[0, 1, 2, 3],
[4, 5, 6, 7]
]

f_norm = sqrt(sum([sum([j**2 for j in i]) for i in a]))

Jan 20, 2023

from math import sqrt
a = [1, 3, -5]
b = [4, -2, -1]
sim = sum([x*y for x,y in zip(a,b)]) / (sqrt(sum([i**2 for i in a])) * (sqrt(sum([i**2 for i in b]))))
from math import acos
theta = acos(sim)

Jan 19, 2023

a = [
[1, 1, 1, 1],
[2, 2, 2, 2]
]

b = [
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4]
]

ds = [row+[0]*len(b[0]) for row in a] + [[0]*len(a[0])+row for row in b]
[
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[2, 2, 2, 2, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 3, 3, 3, 3, 3, 3],
[0, 0, 0, 0, 4, 4, 4, 4, 4, 4]
]

Jan 18, 2023

a = [  
[0, 1, 2, 3, 4, 5],
[5, 4, 3, 2, 1, 0],
[2, 4, 6, 8, 10, 12],
[3, 6, 9, 12, 15, 18]
]

x, y = [0, 2], [1, 4]
sub = [m[y[0]:y[1]] for m in a[x[0]:x[1]]]
[
[1, 2, 3],
[4, 3, 2]
]

Jan 17, 2023

a = [
[1, 2, 3],
[4, 5, 6]
]

b = [
[7, 8, 0],
[9, 10, 0],
[11, 12, 0]
]

result = [[sum([x*y for x,y in zip(row, col)]) for col in zip(*b)] for row in a]

Jan 16, 2023

matrix = [ 
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]

transpose = [[matrix[n][m] for n in range(len(i))] for m, i in enumerate(matrix)]
[
[0, 3, 6],
[1, 4, 7],
[2, 5, 8]
]

Jan 15, 2023

n = 5
matrix = [[1 if i == j else 0 for j in range(n)] for i in range(n)]

Join in…

--

--

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Written by Jeremy Brown

Software Engineer, originally from the U.S. and now living and working in Vienna, Austria. I write about Programming, Music, Machine Learning & NLP

No responses yet

Write a response