擅长:python、mysql、java
<p>似乎您可以从<a href="https://docs.python.org/3/library/calendar.html#calendar.Calendar.itermonthdays4" rel="nofollow noreferrer">^{<cd1>}</a>获取所有日期,并按日期和月份进行筛选。然后用<code>[-2]</code>索引倒数第二个</p>
<pre><code>import calendar
c = calendar.Calendar()
year = 2020
day = 1
for month in range(1, 13):
secondLastTues = list(filter(lambda d:d[3] == day and d[1] == month,
c.itermonthdays4(year, month)))[-2]
print(secondLastTues)
</code></pre>
<p>结果:</p>
<pre><code>(2020, 1, 21, 1)
(2020, 2, 18, 1)
(2020, 3, 24, 1)
(2020, 4, 21, 1)
(2020, 5, 19, 1)
(2020, 6, 23, 1)
(2020, 7, 21, 1)
(2020, 8, 18, 1)
(2020, 9, 22, 1)
(2020, 10, 20, 1)
(2020, 11, 17, 1)
(2020, 12, 22, 1)
</code></pre>