Python 3.x 如何设置“a”的格式;至于;循环查看特定的年度日期范围?

Python 3.x 如何设置“a”的格式;至于;循环查看特定的年度日期范围?,python-3.x,Python 3.x,我必须执行一个python程序来检查从1987年到2012年的年份,该程序必须告诉我在这个范围内有多少年有重复的数字。我正在努力为1987-2012年创造一个循环。在执行代码计算每个数字之前,我必须以某种方式将它们从int转换为字符串 如何将程序重新运行到下一年(例如从1987年到1988年) 结果应该是这样的 1987年没有重复的数字 1988年,8出现了2次 1989年,9出现了2次 对于1990年,9出现了2次在AChampion的评论中展开,您可以像这样重复这些年: start_date

我必须执行一个python程序来检查从1987年到2012年的年份,该程序必须告诉我在这个范围内有多少年有重复的数字。我正在努力为1987-2012年创造一个循环。在执行代码计算每个数字之前,我必须以某种方式将它们从int转换为字符串

如何将程序重新运行到下一年(例如从1987年到1988年)

结果应该是这样的 1987年没有重复的数字 1988年,8出现了2次 1989年,9出现了2次
对于1990年,9出现了2次

在AChampion的评论中展开,您可以像这样重复这些年:

start_date = 1987
end_date = 2013
counter = 0
for year in range(start_date, end_date): # Creates an iterable containing years 1987-2013
    # A set can only have unique items, so it will discard duplicates 
    # If the length of the set is less than 4, then it had to discard a duplicate
    if len(set(str(year))) != 4: 
        counter += 1
print("This date range has", counter, "years with duplicate numbers")

范围内的年份(1987年、2013年):
将为您提供每年的循环。如果
len(set(str(year))!=len(str(year))
,因为集合不计算重复项。如果我使用range(1987、2013),它会给我一个整数格式的年份输出。第二段代码以“cells”开头,向下,可以正常工作,但我必须在顶部手动键入一个字符串形式的年份。你知道我如何写几行代码来迭代1987年到2013年,然后把它们转换成str吗?
year=str(year)
会把它转换成字符串,但看起来你把它变得比需要的复杂得多。我相信。当我输入range(1987年,2013年:然后是str(year)时,它只给我2012年的输出。你知道我这里缺少什么吗?Python是布局敏感的,你必须将所有较低的代码缩进循环
,用于range(1987年,2013年)中的year:
这样它就可以每年运行一次。目前,听起来你好像是在一年一年地运行循环,然后运行代码的其余部分。
start_date = 1987
end_date = 2013
counter = 0
for year in range(start_date, end_date): # Creates an iterable containing years 1987-2013
    # A set can only have unique items, so it will discard duplicates 
    # If the length of the set is less than 4, then it had to discard a duplicate
    if len(set(str(year))) != 4: 
        counter += 1
print("This date range has", counter, "years with duplicate numbers")