使用Python获取数组中特定日期的索引 [英] Get the index of specific date in array using Python

查看:550
本文介绍了使用Python获取数组中特定日期的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有一个日期日期数组= [datetime.date(1962,1,9),datetime.date(1962,1,8),datetime.date(1962,1,5),datetime.date( 1962,1,4),datetime.date(1962,1,3),datetime.date(1962,1,2)]



i想得到索引1962,1,9我怎么做?



我尝试过:



i尝试date.index('1962-1-9')

ValueError:'1962-1-9'不在列表中

hello i have an array of date date=[datetime.date(1962, 1, 9), datetime.date(1962, 1, 8), datetime.date(1962, 1, 5), datetime.date(1962, 1, 4), datetime.date(1962, 1, 3), datetime.date(1962, 1, 2)]

i want to get the index of 1962,1,9 how i can ?

What I have tried:

i tried date.index('1962-1-9')
ValueError: '1962-1-9' is not in list

推荐答案

您必须提供与列表中的值类型相同的值。在您的情况下,所有值都是 datetime.date 类型,因此您的参数必须相同,例如:

You must provide a value that is the same type as the values in the list. In your case all values are datetime.date types, so your parameter must be the same, for example:
date.index(datetime.date(1962, 1, 3))


尝试改为

Try instead
date.index(datetime.date(1962-1-9))





已修复,感谢 Richard


这篇关于使用Python获取数组中特定日期的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆