Skip to content

Instantly share code, notes, and snippets.

@h3ik0th
Created October 15, 2021 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h3ik0th/e631c70dba5e7425babf46bc3c77a534 to your computer and use it in GitHub Desktop.
Save h3ik0th/e631c70dba5e7425babf46bc3c77a534 to your computer and use it in GitHub Desktop.
filter the list of strings:
# example: add "weekend" to Saturdays and Sundays in October of each year
t = time.perf_counter()
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
strLoop = []
for d in datstrlist:
if (d.endswith("urday") or d.endswith("unday")) and "Oc" in d:
strLoop.append(d + " = Oct weekend")
else:
strLoop.append(d)
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
tLoop = time.perf_counter() - t
print(f'{tLoop:.4f} sec')
t = time.perf_counter()
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
c = [d + " = Oct weekend" if ((d.endswith("urday") or d.endswith("unday")) and "Oc" in d) else d for d in datstrlist]
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
tComp = time.perf_counter() - t
print(f'{tComp:.4f} sec: comprehension vs loop: {100*(tComp/tLoop-1):.1f}%')
df = pd.DataFrame(list(zip(strLoop,c)), columns=("loop","comprehension"))
df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment