Created
November 17, 2017 09:02
-
-
Save juanarrivillaga/0e77019d2d8c57f2963976e0cc577291 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# your function works on a single element in your container | |
def converter(period): | |
period = period.lower() | |
if period[-1:] == "d": | |
period_unit = 1 | |
elif period[-1:] == "w": | |
period_unit = 7 | |
elif period[-1:] == "m": | |
period_unit = 30 | |
elif period[-1:] == "y": | |
period_unit = 360 | |
else: | |
print(period) | |
raise Exception("Period not recognized!") | |
return 10*period_unit | |
# here we use a list | |
period_list = ['1D', '1W', '1M', '1Y'] | |
# most basic way: for-loop | |
new_list = [] | |
for p in period_list: | |
new_list.append(converter(p)) | |
print(new_list) | |
# using a list-comprehension that is equivalent | |
# to the above for-loop | |
new_list2 = [converter(p) for p in period_list] | |
print(new_list2) | |
# using map | |
new_list3 = list(map(converter, period_list)) | |
print(new_list3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment