简体   繁体   English

AttributeError:'dict'对象没有属性'append'

[英]AttributeError: 'dict' object has no attribute 'append'

Can someone please tell me what basic thing I am missing here.

Type: <class 'list'>
Value : ['09,10,11,12,13,14,15']

for datapoint in value:
    y.append(datetime.fromtimestamp(datapoint).strftime('%I%P').lstrip('0').upper())

I want value of y should be like this-[9PM,10PM,11PM,12PM,1PM,2PM,3PM]

I am not sure why its is not converting to the value I want, if I am using above function. 我不确定如果使用上面的函数,为什么它没有转换成我想要的值。 Can someone please suggest what I am missing here and why I am getting this error -> "AttributeError: 'dict' object has no attribute 'append'" 有人可以建议我在这里缺少什么以及为什么会出现此错误->“ AttributeError:'dict'对象没有属性'append'”

You have a 1-element list with a string in it: your datapoint is the single whole string, not pieces of it. 您有一个包含一个字符串的1元素列表:您的datapoint是单个完整的字符串,而不是其中的一部分。 You need to split and iterate over the splitted values: 您需要拆分并迭代拆分后的值:

from datetime import datetime

y = [] # use list to use append, see dict approach below

data = '09,10,11,12,13,14,15'.split(",") #split into ["09","10",...,"15"]

for dp in data: # "09" then "10" then "11" etc.
    y.append(datetime.strptime(dp,"%H").strftime('%I%P').strip("0").upper())

print(y)

Output: 输出:

['9AM', '10AM', '11AM', '12PM', '1PM', '2PM', '3PM']

To add that do a dictionary you need to use update((key,value)-iterable) or d[key]=value : 要添加该字典,您需要使用update((key,value)-iterable)d[key]=value

d = {}
for time in y:
    d["Time "+time] = time

# or

d.update(  ((t,t) for t in y) ) # doesnt make much sense to have identical key/values

# d[]=... - Output
{'Time 9AM': '9AM', 'Time 12PM': '12PM', 'Time 3PM': '3PM', 
 'Time 11AM': '11AM', 'Time 2PM': '2PM', 'Time 10AM': '10AM', 
 'Time 1PM': '1PM'}

# update - Output 
{'12PM': '12PM', '1PM': '1PM', '11AM': '11AM', '9AM': '9AM', 
 '10AM': '10AM', '3PM': '3PM', '2PM': '2PM'}

The error is pretty clear, you are trying to use append() on the variable y which is here a dictionary. 错误非常明显,您尝试在变量y上使用append() ,这里是字典。 Dictionaries do not have an append() function, hence the error. 字典没有append()函数,因此会出现错误。

In order for your code to work, you probably need y to be a list . 为了使代码正常工作,您可能需要y作为list

暂无
暂无

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

相关问题 Python AttributeError: &#39;dict&#39; 对象没有属性 &#39;append&#39; - Python AttributeError: 'dict' object has no attribute 'append' AttributeError: 'dict' object 在 json 中没有属性 'append' - AttributeError: 'dict' object has no attribute 'append' in json AttributeError:&#39;dict&#39;对象没有属性&#39;append&#39;试图写入.JSON文件 - AttributeError: 'dict' object has no attribute 'append' Trying to write to .JSON file Smartsheet数据跟踪器:AttributeError:&#39;dict&#39;对象没有属性&#39;append&#39; - Smartsheet Data Tracker: AttributeError: 'dict' object has no attribute 'append' 不理解 Python 中的错误消息:AttributeError: &#39;dict&#39; object has no attribute &#39;append&#39; - Not understanding error message in Python: AttributeError: 'dict' object has no attribute 'append' AttributeError: 'dict' object 没有属性 'append' Python 和 Json - AttributeError: 'dict' object has no attribute 'append' Python and Json AttributeError: 'dict' object 没有属性 - AttributeError: 'dict' object has no attribute 如何将值附加到字典键? (AttributeError:“ str”对象没有属性“ append”) - How do I append a value to dict key? (AttributeError: 'str' object has no attribute 'append') AttributeError:“字节”对象没有属性“ __dict__” - AttributeError: 'bytes' object has no attribute '__dict__' AttributeError: &#39;dict&#39; 对象没有属性 &#39;iterkeys&#39; - AttributeError: 'dict' object has no attribute 'iterkeys'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM