简体   繁体   English

在python中创建此字典的最佳方法

[英]best way to create this dict in python

this is my code : 这是我的代码:

vars_ = {
                'attackUp':attackUp,'defenceUp':defenceUp,'magicUp':magicUp,'attType':attType,'weightDown':weightDown,
                'accAttackSword':accAttackSword,'accAttackSaber':accAttackSaber,'accAttackAx':accAttackAx,
                'accAttackHammer':accAttackHammer,'accAttackSpear':accAttackSpear,'accAttackFight':accAttackFight,
                'accAttackBow':accAttackBow,'accAttackMagicGun':accAttackMagicGun,'accAttackMagic':accAttackMagic,
                'mStrInstrument':mStrInstrument,'mStrCharms':mStrCharms,'accDefencePhy':accDefencePhy,
                'accDefenceMag':accDefenceMag,'accWeight':accWeight,'bookTurn':bookTurn,'bookAttackPhy':bookAttackPhy,
                'bookAttackMag':bookAttackMag,'bookStrInstrument':bookStrInstrument,'bookStrCharms':bookStrCharms,
                'bookDefencePhy':bookDefencePhy,'bookDefenceMag':bookDefenceMag,'bookWeight':bookWeight,'name':name,
                'plvl':plvl,'str':str,'ski':ski,'mag':mag,'spd':spd,'locX':locX,'locY':locY,'wName':wName,
                'wAttack':wAttack,'wDefence':wDefence,'wWeight':wWeight,'wType':wType,'target':target,'title':title,
                'uname':uname,'cUrl':cUrl,'mbCnt':mbCnt
                }

oh my god , I spent a lot of time on this work , and maybe have more Variable to be added later , 哦,天哪,我在这项工作上花了很多时间,也许以后还要添加更多变量,

any easy way to do this , 任何简单的方法,

thanks 谢谢

I would stop and consider why you are doing this. 我停下来考虑一下为什么要这样做。 I can't help but think its not necessary. 我忍不住认为没有必要。

Even if you decide this is necessary (which i doubt) - You are pretty much recreating globals() . 即使您确定这是必要的(我对此表示怀疑),也几乎要重新创建globals() Type that into your interpretter and see if you still want to do this. 在您的解释器中键入该内容,然后查看是否仍要执行此操作。

Organize it further like senderle suggested in your other post. 像其他帖子中建议的senderle一样,进一步组织它。 And maybe post a broader question with help for organizing your project. 也许发布一个更广泛的问题,以帮助您组织项目。

The first thing I would do is reformat that dictionary so there is one entry per line: 我要做的第一件事是重新格式化该字典,以便每行有一个条目:

vars_ = {
    'attackUp'  : attackUp,
    'defenceUp' : defenceUp,
    'magicUp'   : magicUp,
    'attType'   : attType,
    'weightDown': weightDown,
    # and so on
}

I have also lined up the columns so the whole list reads more easily. 我还对各列进行了排列,因此整个列表更易于阅读。

You could make an array of variable names and pull them out of the locals dictionary. 您可以创建一个变量名数组,然后将其从locals字典中拉出。

x, y, z = 5, 10, 20
l = locals()
d = {}
for v in ['x', 'y', 'z']:
    d[v] = l[v]
# d = {'y': 10, 'x': 5, 'z': 20}

locals might work on it's own too if you're just wanting to look it up as a string. 如果您只是想将其查找为字符串,那么locals也可以自己使用。

attUp = locals()['attackUp']

I totally agree with @miku - look at how you are using the values and seriously refactor. 我完全同意@miku-看一下您如何使用这些值并认真重构。

For example, a Character has Attributes (physical_attack, physical_defence, magic_attack, magic_defence, weight, speed) and Items; 例如,角色具有属性(物理攻击,物理防御,魔术攻击,魔术防御,重量,速度)和物品; Weapons are Items, Swords and Axes and Spears and Bows are Weapons, a Saber is a Sword. 武器是物品,剑和斧头,长矛和弓箭是武器,军刀是剑。 Unarmed is a special default Weapon. 未武装是特殊的默认武器。 Charms are Items, but apparently Books and StringedInstruments are Weapons?? 魅力是物品,但显然书籍和StringedInstruments是武器? Items have Attributes which are added to a Character's Attributes while equipped. 物品的属性会在装备时添加到角色的属性中。 Character also has level, location, target, and an accuracy rating for each weapon type (can a Weapon have an accuracy-modifier?). 角色还具有每种武器类型的等级,位置,目标和准确性等级(武器可以具有准确性修饰符吗?)。

If you break it down into a class hierarchy this way, it should be much easier to keep track of what you are doing. 如果您以这种方式将其分解为类层次结构,那么跟踪您的工作将更加容易。

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

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