Time travel - how to simplify?
Peter Otten
__peter__ at web.de
Sat Nov 18 18:52:11 EST 2017
Andrew Z wrote:
> well, yeah, it's unidirectional and final destination is always the same
> and have little to do with the question.
>
> Say, i have a dict:
>
> fut_suffix ={ 1 : 'F',
> 2 : 'G',
> 3 : 'H',
> 4 : 'J',
> 5 : 'K',
> 6 : 'M',
> 7 : 'N',
> 8 : 'Q',
> 9 : 'U',
> 10: 'V',
> 11: 'X',
> 12: 'Z'
> }
>
> where key is a month.
> Now i want to get certain number of months. Say, i need 3 months duration
> starting from any month in dict.
>
> so if i start @ 7th:
> my_choice =7
> for mnth, value in fut_suffix:
> if my_choice >= mnth
> # life is great
> but if :
> my_choice = 12 then my "time travel" becomes pain in the neck..
>
> And as such - the question is - what is the smart way to deal with cases
> like this?
Make a lookup list that is big enough for your application and then use
slicing:
>>> def months(first, count, lookup=list("FGHJKMNQUVXZ" * 3)):
... start = first - 1
... return lookup[start: start + count]
...
>>> months(3, 3)
['H', 'J', 'K']
>>> months(12, 3)
['Z', 'F', 'G']
More information about the Python-list
mailing list