Created
October 15, 2021 15:57
-
-
Save h3ik0th/e631c70dba5e7425babf46bc3c77a534 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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