【问题标题】:How to append an item to a dictionary in a list?如何将项目附加到列表中的字典?
【发布时间】:2020-08-19 01:52:43
【问题描述】:

您好,我正在开发一个基于文本的游戏,但我正在努力使用 drop 命令;它的工作方式是你写“D 物品的名称”然后它检查物品是否真的在库存中,如果是,它把它放在一个变量中,从库存中删除它并我希望它附加到房间(字典)的内容索引中,并且该字典在列表中,我无法附加到它。 这是代码(其中一些):

room = []
room.append({'number': 1, 'content': ""})
roomnumber = 1
inv = ["sword"]
command = input(": ")
first_letter = command(0)

if first_letter == "D":
   item = command.split(" ", 2)
   item.remove("D")
   for i in range(0, len(inv):
      inv.pop(i)
#this doesn't work
`     room[roomnumber]['content'].append(item[0])`
      item.pop(0)

在我输入:“D剑”之后,它给了我这个错误:

Traceback (most recent call last):
File "/Users/antony/PycharmProjects/TextBased/Main.py", line 54, in <module>
room[roomnumber]['content'].append(item[0])
AttributeError: 'str' object has no attribute 'append'

没看懂,求大神帮忙!

【问题讨论】:

  • 查看我的所有 cmets 以及在我的答案中作为 cmets 添加为代码中的代码的更正。主要问题是您将列表方法 (append()) 应用于字典对象。

标签: python dictionary input data-structures append


【解决方案1】:

好的,非常感谢大家 :),我只是想说有些事情有点奇怪的原因是因为这只是代码的 20% 左右(例如,女巫的房间更多,这就是我需要在列表中使用字典)并且我的房间号在我的实际代码中确实以 0 开头:),pop 是从库存中删除该项目(因为这是 drop 命令),我将它从 item 变量中删除只是为了安全,它不会导致任何不需要的错误。否则,是的,内容应该是一个列表,我忘记了,感谢您指出它,并且 for 循环实际上已关闭它(我写这篇文章时有点着急)。无论如何,谢谢大家:)

【讨论】:

  • 不用担心 :) 在这种情况下,我仍然会使用我答案的最后一个命题,即只使用一个全局字典并通过 room[roomnumber] = content 定义房间/内容。在结构方面要容易得多,这使您的任何操作都不太容易出错:)
  • 哦,请接受适合你的答案,这样我们也得到一些回报?
【解决方案2】:
room = []
room.append({'number': 1, 'content': ""})
## room = [{'number': 1, 'content': ""}]

roomnumber = 1
## you should actually change this to 0. Otherwise you will get an "index out
## of range" error (see comment below)

inv = ["sword"]
command = input(": ")
first_letter = command(0)

if first_letter == "D":
   item = command.split(" ", 2)
## why are you adding a max split here??

   item.remove("D")
   for i in range(0, len(inv)):
## equals for i in range(0, 5):, so iterates 0,1,2,3,4
## you forgot to add a closing bracket for the range

      inv.pop(i)
## what are you trying to do here? This looks strange to me, given that after each
## pop, the value of your inv will be shortened..?

#this doesn't work
      room[roomnumber]['content'].append(item[0])
## this does not work because your room list only contains one element, a 
## dictionary, at index position 0. Probably THIS is your biggest issue here. 
## Second, you're trying to change the value of 'content'. To change the value of a
## dictionarie's key, you need to use "=", and not ".append", as ".append" is used
## to append an element at the end of a list, to a list, and not to a dictionary. 
## So, use room[roomnumber]['content'] = item[0] 
      item.pop(0)

据我了解,您想在房间号的功能中将内容添加到相应房间号的字典的内容值中。在这种情况下,您的整个语法都是错误的,您必须使用:

room = []
room.append({1:""})

## and then, after the rest of the code:

room[roomnumber-1][roomnumber] = item[0]

或者,甚至更简单,因为这里同时使用列表和字典实际上已经过时了

## initiate one dictionary, containing all the rooms in key = roomnumber
## value = content pairs
rooms = {}

## the syntax to add a new room number with a new content into the dictionary
## would then simply be: rooms[number] = content, e.g.:
rooms[1] = ""

## to then set the value of the content for a given roomnumber, you simply use
rooms[roomnumber] = item[0]

我建议您了解python中列表和字典之间的基本区别,您似乎对如何访问/修改列表和字典的元素缺乏一些基本的了解(当然没有冒犯)

【讨论】:

    【解决方案3】:

    您想要一个能够容纳不止一件东西的房间吗?如果是这样,请将您的 content 字段设为列表而不是字符串:

    room.append({'number': 1, 'content': []})
    

    现在你可以append任意数量的东西到content

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 2020-04-09
      • 2015-08-22
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多