清洁方式将物品从一个清单带到另一个清单 [英] Cleaner Way to Take Items from One List to Another

查看:90
本文介绍了清洁方式将物品从一个清单带到另一个清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在写一个文字冒险游戏,有一次我需要从一个列表中取一个由用户输入给出的项目并将其移动到另一个列表。具体来说,除了以下内容之外,有什么方法可以获得项目的索引:

I've been writing a text adventure game, and at one point I need to take an item, which is given by user input, from one list and move it to another list. Specifically, is there any way to get the index of an item when you know the item name besides something like:

list_one = ["item one", "item two"]
index_one = list_one.index("item one")

我在我的脚本中使用的代码是:

The code I'm using in my script is:

player.items.append(start_room.items.pop(start_room.items.index(next)))

接下来是输入,这似乎很乱。如果有更简单的方法可以解决这个问题,请告诉我。谢谢!

Where next is the input, and this seems very messy. If there's an easier way to go about this, let me know. Thanks!

推荐答案

如果您已经知道该项目,则无需拨打 index pop 或其他:

If you already know the item, there is no need to call index or pop or whatever:

list_one.remove (item)
list_two.append (item)

这篇关于清洁方式将物品从一个清单带到另一个清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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