用相同的列表數據變量,但不同的對象,這是我的代碼:定義使用python
attackUp = [10, 15,10, 15,10, 15]
defenceUp = [10, 15,10, 15,10, 15]
magicUp = [10, 15,10, 15,10, 15]
attType = [1,1,1,1,1,1]
weightDown = [10, 15,10, 15,10, 15]
#裝飾器數據
accAttackSword = [100, 100,100, 100,100, 100]
accAttackSaber = [100, 100,100, 100,100, 100]
accAttackAx = [100, 100,100, 100,100, 100]
accAttackHammer = [100, 100,100, 100,100, 100]
accAttackSpear = [100, 100,100, 100,100, 100]
accAttackFight = [100, 100,100, 100,100, 100]
accAttackBow = [100, 100,100, 100,100, 100]
accAttackMagicGun = [100, 100,100, 100,100, 100]
accAttackMagic = [100, 100,100, 100,100, 100]
mStrInstrument = [100, 100,100, 100,100, 100]
mStrCharms = [100, 100,100, 100,100, 100]
accDefencePhy = [100, 100,100, 100,100, 100]
accDefenceMag = [100, 100,100, 100,100, 100]
accWeight = [100, 90, 0, 0, 100, 90]
#戰術書數據
bookTurn = [1,1]
bookAttackPhy = [100, 100]
bookAttackMag = [100, 100]
bookStrInstrument = [100, 100]
bookStrCharms = [100, 100]
bookDefencePhy = [100, 100]
bookDefenceMag = [100, 100]
bookWeight = [100, 100]
你可以看到:許多變量具有相同的價值,但我不能確定他們是這樣的:
bookAttackPhy = bookAttackMag =bookStrInstrument=bookStrCharms=bookDefencePhy=[100, 100]
因爲如果其中一個變化,所有變化。
哪個是定義這些變量最好也最容易的?
您是否定義了6個不同字符的特徵?並使用索引0到5訪問給定字符的特定屬性?定義一個類並使這些實例屬性!這是面向對象的原因! – PaulMcG 2011-03-22 03:40:36
[如何在Python中複製或複製列表?](http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list-in-python) – 2015-04-03 15:18:13
@ArtjomB 。:複製列表是一種解決方法。針對OP問題的有效解決方案是創建一個像@Paul McGuire建議的自定義對象,並提供枚舉其屬性的方法,例如'爲了自己的名字。_skill_names:setattr(self,name,[100] * 5)' – jfs 2015-04-05 12:05:42