简体   繁体   English

关联 Python 上的天数和年数

[英]Correlate days & number of year on Python

I need to itinerate the days of the week on the following tuple and assign it to a day of the year (from 1 to 365).我需要在以下元组上循环一周中的几天并将其分配给一年中的一天(从 1 到 365)。 I have to do it without transforming the tuple in a list.我必须在不转换列表中的元组的情况下做到这一点。 Of course, my first attempt was to transform the tuple on a list, extend it and use zip() function.当然,我的第一次尝试是转换列表中的元组,扩展它并使用 zip() function。 Any ideas of how to do it without transforming the tuple with loops?关于如何在不使用循环转换元组的情况下如何做到这一点的任何想法?

Thanks a lot!非常感谢!

days = ("lunes","martes","miércoles","jueves","viernes","sábado","domingo")
list_days = list(days)
number = list(range(1,366))
list_compl = list_days*53
list_compl

for j,i in zip(list_compl,number):
    print(i,j)

To cycle over the list of days, you can use the cycle function from the itertools package.要循环查看日期列表,您可以使用itertools package 中的cycle function。

import itertools

days = ("lunes", "martes", "miércoles", "jueves", "viernes", "sábado", "domingo")
number = range(1, 366)

for j, i in zip(itertools.cycle(days), number):
    print(i, j)
``

An even simpler answer than the one from @thomas_bssnt could be to do this比来自@thomas_bssnt 的答案更简单的答案可能是这样做

days = ("lunes", "martes", "miércoles", "jueves", "viernes", "sábado", "domingo")

for i in range(1, 366):
    print(i, days[i%7-1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM