将数组编号元素交换为其他数组的字符串

时间:2016-05-16 13:31:37

标签: python arrays

我有两个数组:

months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
current_month = datetime.date.today().month
last_months = [(current_month - i - 1) % len(months) + 1 for i in range(7)]

我想将last_months数组号码交换为months的字符串。 我怎么能这样做?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以将last_months的索引器部分包装在数组查找中。

[months[(current_month - i - 1) % len(months) + 1] for i in range(7)]