如何根据Python中的变量值创建一个字典 [英] How to create a dictionary based on variable value in Python

查看:1898
本文介绍了如何根据Python中的变量值创建一个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建名称来自变量的字典。

I am trying to create a dictionary where the name comes from a variable.

这是情况,因为也许有更好的方法:
我使用API​​来获取对象的属性。 (名称,说明,X,Y,Z)等。我想以对象保存数据的方式存储此信息。

Here is the situation since maybe there is a better way: Im using an API to get attributes of "objects". (Name, Description, X, Y, Z) etc. I want to store this information in a way that keeps the data by "object".

为了获得这个信息,API会遍历所有的对象。

In order to get this info, the API iterates through all the "objects".

所以我的建议是,如果对象名称是我想要捕获的对象之一,我想用这样的名字创建一个字典:

So what my proposal was that if the object name is one of the ones i want to "capture", I want to create a dictionary with that name like so:

ObjectName = {'Description': VarDescrption, 'X': VarX.. etc}

(我在哪里说Varetc ... / code>这将是API传递的该属性的值。

(Where I say "Varetc..." that would be the value of that attribute passed by the API.

现在,由于我提前知道名单,我可以使用真的很长如果树,但我正在寻找一些更容易编写代码来实现这一点的东西(和可扩展的,而不增加太多的代码)

Now since I know the list of names ahead of time, I CAN use a really long If tree but am looking for something easier to code to accomplish this. (and extensible without adding too much code)

这里是我有的代码:

def py_cell_object():
    #object counter - unrelated to question
    addtototal()
    #is this an object I want?
    if aw.aw_string (239)[:5] == "TDT3_":
        #If yes, make a dictionary with the object description as the name of the dictionary.
        vars()[aw.aw_string (239)]={'X': aw.aw_int (232), 'Y': aw.aw_int (233), 'Z': aw.aw_int (234), 'No': aw.aw_int (231)}
        #print back result to test
        for key in aw.aw_string (239):
            print 'key=%s, value=%s' % (key, aw.aw_string (239)[key])

这里是前两行的代码,以显示什么aw是

here are the first two lines of code to show what "aw" is

from ctypes import *
aw = CDLL("aw")

来解释API调用中的数字:

to explain what the numbers in the API calls are:

231 AW_OBJECT_NUMBER,

231 AW_OBJECT_NUMBER,

232 AW_OBJECT_X,

232 AW_OBJECT_X,

233 AW_OBJECT_Y,

233 AW_OBJECT_Y,

234 AW_OBJECT_Z,

234 AW_OBJECT_Z,

239 AW_OBJECT_DESCRIPTION,

239 AW_OBJECT_DESCRIPTION,

231-234是整数和239是一个字符串

231-234 are integers and 239 is a string

推荐答案

我推断你正在使用 Active Worlds SDK 。这将节省时间提到在未来的问题的第一位。

I deduce that you are using the Active Worlds SDK. It would save time to mention that in the first place in future questions.

我猜你的目标是创建一个顶级字典,其中每个键是对象描述。每个值都是另一个字典,存储该对象的许多属性。

I guess your goal is to create a top-level dictionary, where each key is the object description. Each value is another dictionary, storing many of the attributes of that object.

我快速浏览了维基上的AW SDK文档,我看不到请求SDK查询属性名称,ID和类型的列表。所以你将不得不在你的程序中硬编码这些信息。除非您在其他地方需要它,否则只需在创建字典时对其进行硬编码,这是最简单的,这正是您正在做的。要打印出来,只需打印属性字典的 repr 。我可能格式化你的方法更像这样:

I took a quick look at the AW SDK documentation on the wiki and I don't see a way to ask the SDK for a list of attribute names, IDs, and types. So you will have to hard-code that information in your program somehow. Unless you need it elsewhere, it's simplest to just hard-code it where you create the dictionary, which is what you are already doing. To print it back out, just print the attribute dictionary's repr. I would probably format your method more like this:

def py_cell_object():
    #object counter - unrelated to question
    addtototal()

    description = aw.aw_string(239)
    if description.startswith("TDT3_"):
        vars()[description] = {
            'DESCRIPTION': description,
            'X': aw.aw_int(232),
            'Y': aw.aw_int(233),
            'Z': aw.aw_int(234),
            'NUMBER': aw.aw_int (231),
            ... etc for remaining attributes
        }

        print repr(vars()[description])

有些人会认为你应该为232,233,234,等等,但是我看不到什么理由去做,除非你在多个地方需要它们,否则很容易从SDK自动生成它们(例如,通过解析一个 .h 文件)。

Some would argue that you should make named constants for the numbers 232, 233, 234, etc., but I see little reason to do that unless you need them in multiple places, or unless it's easy to generate them automatically from the SDK (for example, by parsing a .h file).

这篇关于如何根据Python中的变量值创建一个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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