Python 使用numpy计算给定dd/mm(给定1月1日)的星期几

Python 使用numpy计算给定dd/mm(给定1月1日)的星期几,python,numpy,Python,Numpy,我正在做一篇复习考试的过去的论文,我被这个问题困住了: 编写一个python脚本,从用户处接受特定年份中1月1日的星期几,然后打印用户指定的任何其他月份和月份的星期几。您可以假设该年不是闰年,用户输入的输入为任何单词的前三个字母,并且之前定义了以下代码: import numpy as np months = np.array(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']) ndays = np

我正在做一篇复习考试的过去的论文,我被这个问题困住了:

编写一个python脚本,从用户处接受特定年份中1月1日的星期几,然后打印用户指定的任何其他月份和月份的星期几。您可以假设该年不是闰年,用户输入的输入为任何单词的前三个字母,并且之前定义了以下代码:

import numpy as np
months = np.array(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'])
ndays = np.array([31,28,31,30,31,30,31,31,30,31,30,31])
days=np.array(['Mon','Tue','Wed','Thu','Fri','Sat','Sun'])
因此,我知道如何使用input命令从用户身上获取输入,这样我就可以创建3个变量:

user_Jan_1_day = input('Input the day of the week for Jan 1st: ')
user_month = input('Input the month: ')
user_day = input('Input the day of the month required: ')
假设用户说1月1日是“星期六”,他们希望一周中的哪一天是“3月1日”,即3月1日

我知道我需要31+28+1。求和=60。 取模数:60%7=4,然后在“Sat”中加上四周的天数,得到“Wed”作为我的答案,但是我如何在Python中做到这一点呢

我想我可以通过使用数组的索引来实现这一点,所以我使用

a=np.where(months==user_month)
no_of_days = 0
for i in range (a):
    no_of_days =+ ndays[i]
但我得到一个错误:“'tuple'对象不能解释为整数”

有人能告诉我怎么做吗


谢谢大家!

按照您的确切逻辑,您可以这样做,而无需迭代(确保为
user\u day
获取
int(输入(…)
),而不是默认的字符串输入):

例如

>>> user_Jan_1_day = input('Input the day of the week for Jan 1st: ')
Input the day of the week for Jan 1st: Sat
>>> user_month = input('Input the month: ')
Input the month: Mar
>>> user_day = int(input('Input the day of the month required: '))
Input the day of the month required: 1

>>> m = np.where(months == user_month)[0][0]
>>> d = np.where(days == user_Jan_1_day)[0][0]
>>> result = days[(np.sum(ndays[:m]) + user_day + d) % 7]

>>> result
'Wed'
range()
函数采用整数。但是
a
本身不是一个整数。因此,尝试使用
范围(a[0][0])

此外,您的“=+”应该是“+=”,如下所示


这将为您提供所需的日期,作为日历年的天数。从那里,您可以使用一些模运算(“
%
”)来计算1月1日的天数,而不是
天数
数组。

非常感谢。我使用帮助找到了np.where函数,但并不知道如何使用它。当我发现np.where返回一个元组时,我放弃了。我不知道我是否可以使用这些索引功能来进一步。非常感谢。我感谢你花时间帮助我。我知道逻辑,但没有编程技巧。我将研究一下答案,把它拆开,等等,这样我就知道将来如何将np.where()与索引一起使用。
>>> user_Jan_1_day = input('Input the day of the week for Jan 1st: ')
Input the day of the week for Jan 1st: Sat
>>> user_month = input('Input the month: ')
Input the month: Mar
>>> user_day = int(input('Input the day of the month required: '))
Input the day of the month required: 1

>>> m = np.where(months == user_month)[0][0]
>>> d = np.where(days == user_Jan_1_day)[0][0]
>>> result = days[(np.sum(ndays[:m]) + user_day + d) % 7]

>>> result
'Wed'
for i in range(range(a[0][0])):
    no_of_days += ndays[i]
no_of_days += int(user_day)