简体   繁体   English

如何将输入值附加到字典中的列表?

[英]How to append an input value to a list in a dictionary?

I'm trying to append a value entered by the user to list in a dictionary, but it shows error: 我正在尝试附加用户输入的值以在字典中列出,但是它显示错误:

AttributeError: 'str' object has no attribute 'append' AttributeError:'str'对象没有属性'append'

Can anyone find out the mistake? 谁能找出错误?

Dict = {} # an empty dictionary to be filled later

Dict["SomeKey"] = []

Dict["SomeKey"] = input ("Enter a value: ") # it works

Dict["SomeKey"].append(input("Enter another value: ")) # This part gives me error !!!

AttributeError: 'str' object has no attribute 'append' AttributeError:'str'对象没有属性'append'

You may want to use it like so: 您可能想像这样使用它:

dict["SomeKey"] = [input ("Enter a value: ")]
dict["SomeKey"].append(input('Yet again...'))

Because the function input returns a string which means dict["SomeKey"] is also a string which doesn't have the append function. 因为函数input返回一个字符串,这意味着dict["SomeKey"]也是没有append函数的字符串。

This trace back will help you to resolve your problem. 此追溯将帮助您解决问题。

>>> Dict = {}
>>> Dict["SomeKey"] = []
>>> type(Dict["SomeKey"])
list
>>> Dict["SomeKey"] = input ("Enter a value: ")  # in here you are change `list` to `str`
Enter a value: 123
>>> type(Dict["SomeKey"])
str

So the error is correct 'str' object has no attribute 'append' . 因此错误是正确的'str' object has no attribute 'append' append is available on list . list可以append

>>> 'append' in dir(str)
False
>>> 'append' in dir(list)
True

So if you want to keep Dict["SomeKey"] as a list , just change it like which you already did in your last line. 因此,如果要将Dict["SomeKey"]保留为list ,只需像在上一行中所做的那样进行更改即可。

I have written following code and it works fine as long as you already have that "SomeKey" in dictionary and you are entering the user inputs in double quotes. 我已经编写了以下代码,只要您在字典中已经有“ SomeKey”并且您正在用双引号输入用户输入,它就可以正常工作。

Dict = {}
Dict["SomeKey"] = []
Dict["SomeKey"].append(input("Enter another value:"))
Dict["SomeKey"].append(input("Enter another value:"))
print Dict

O/P
sankalp-  ~/Documents  python p.py                                                                                                            
 ✔  2027  00:59:30
Enter another value:"SomeValue1"
Enter another value:"Somevalue2"

{'SomeKey': ['SomeValue1', 'Somevalue2']}

From the previous part of your example you set Dict["SomeKey"] to a string. 在示例的上一部分中,将Dict["SomeKey"]设置为字符串。

Let's say that you typed in "foo" for the entry in step 3 in your example, then Dict["SomeKey"].append("another_string") (I used "another_string" as the result of what you might have typed for the input). 假设您在示例中的第3步中输入了“ foo”作为条目,然后输入了Dict["SomeKey"].append("another_string") (由于您可能会输入“ another_string”,输入)。 Then, this becomes "foo".append("another_string). But "foo", a string, does not have an .append() method. 然后,它变成“ foo” .append(“ another_string)。但是” foo“是一个字符串,没有.append()方法。

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

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