简体   繁体   English

'datetime.datetime'对象不可下标

[英]'datetime.datetime' object is not subscriptable

I've checked every other post about this, but none can fix my issue. 我检查了所有其他与此相关的帖子,但是没有一个可以解决我的问题。

I made a list that holds tuples with an id and a datetime object. 我做了一个列表,其中包含一个带id和datetime对象的元组。 Everytime I try to clean up the list with: last_encounters = [item for item in last_encounters if item[1] < datetime.utcnow] I get the error that 'datetime.datetime' object is not subscriptable . 每当我尝试使用以下方法清理列表时: last_encounters = [item for item in last_encounters if item[1] < datetime.utcnow]我收到'datetime.datetime' object is not subscriptable It's getting pretty annoying, I tried dicts.. didn't work. 它变得非常烦人,我尝试了字典。

Also tested the item[1], according to my print it is a datetime. 还测试了项目[1],根据我的打印,它是一个日期时间。

Even tried changing it to (x,y) for x,y in last_encounters if y < ... also did NOT work. (x,y) for x,y in last_encounters if y < ...也不起作用(x,y) for x,y in last_encounters if y < ...甚至尝试将(x,y) for x,y in last_encounters if y < ...更改为(x,y) for x,y in last_encounters if y < ...

Some useful code: 一些有用的代码:

list = []
d_t = datetime.utcfromtimestamp(9000000)     
list += [('lel', d_t)]     
list = [item for item in list if item[1] < datetime.utcnow]

I hope someone can tell me what I am doing wrong here. 我希望有人能告诉我我在做什么错。

Thanks in advance, 提前致谢,

Kevin 凯文

When you do last_encounters += (a, b) , you are adding two sequences together, last_encounters and (a,b) . 当您执行last_encounters += (a, b) ,您将两个序列相加在一起,即last_encounters(a,b) This means you end up with a and b stuck on the end of the list, rather than just adding the tuple to the list. 这意味着您最终将ab停留在列表的末尾,而不仅仅是将元组添加到列表中。

There are two options to fix your problem: 有两种方法可以解决您的问题:

  1. Add a sequence containing your tuple: 添加一个包含您的元组的序列:

      last_encounters += [(d["id"], d["d_t"])] 
  2. Or preferably, use the append method: 或者最好使用append方法:

      last_encounters.append((d["id"], d["d_t"])) 

It looks like your problem is the way you add the tuple to the list. 看来您的问题是将元组添加到列表的方式。 Here is an example to show the problem : 这是显示问题的示例:

l = []
l += ("a", "b")
print l

l = []
l.append( ("a", "b"))
print l  

Which gives : 这使 :

>>> ['a', 'b']
>>> [('a', 'b')]

So list+=tuple is equivalent to calling list.extend(tuple) and not list.append(tuple) which is what you want. 所以list+=tuple等效于调用list.extend(tuple)而不是list.append(tuple) ,这就是您想要的。

A side note on the meaning of the exception that was raised : X is not subscriptable means that your are trying to call that syntax X[some int] while the object doesn't support it. 关于引发的异常的含义的补充说明: X is not subscriptable意味着您正在尝试调用该语法X[some int]而对象不支持该语法。

Try calling utcnow as a method utcnow() : 尝试调用utcnow作为方法utcnow()

last_encounters = [item for item in last_encounters if item[1] < datetime.utcnow()]

I couldn't reproduce your error with a version of your code, but a version with items in the list lead to this fix. 我无法使用您的代码版本重现您的错误,但是列表中包含项目的版本可以解决此问题。

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

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