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 7

Jeremy Brown
Python in Plain English
5 min readFeb 12, 2023

--

Photo by Matteo Grassi on Unsplash

Feb 18, 2023

from csv import reader
from itertools import chain, permutations

with open('./data/csv/songs.csv') as f:
r = 2
perms = permutations(
chain.from_iterable([row[0].split(' ') for row in reader(f)]), r
)
Left -> Right, Top -> Bottom: Louis Squarepusher, Flying Rachmoninov, Collective Louis, Mouse Symphony, Nino Oval, Mouse Bowie, Twin Beatles

Feb 17, 2023

from itertools import dropwhile, takewhile
from string import ascii_lowercase as chars

start, end = 'd', 'm'
filtered = list(
dropwhile(lambda row: chars.index(row[0][0].lower()) < chars.index(start),
takewhile(lambda row: chars.index(row[0][0].lower()) <= chars.index(end), data)
)
)

Feb 16, 2023

from csv import reader
from itertools import groupby

with open('./data/csv/songs.csv') as f:
groups = groupby(
sorted(reader(f), key=lambda row: row[3]), lambda row: row[3]
)

Feb 15, 2023

from itertools import accumulate, repeat

lm = lambda r: [
round(x, 2) for x in accumulate(repeat(0.1, 100), lambda x, _: r*x*(1-x))
]

Feb 14, 2023

from itertools import accumulate

days = 100
smoothing = 2

ema = list(
accumulate(
data,
lambda a, b: (b * (smoothing / (1+days)) + (a * (1-(smoothing / (1+days)))))
)
)

Feb 13, 2023

from itertools import islice

n = 6
it = iter([i for i in range(100)])
while batch := tuple(islice(it, n)): process(batch)

Feb 12, 2023

from itertools import starmap
from math import pow

N = 16
p2 = [x for x in starmap(pow, [(2, i) for i in range(0, N)])]

--

--

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