0

this is my code :

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]

you can see that : Many variables has the same value , but i cant define them like this :

bookAttackPhy = bookAttackMag =bookStrInstrument=bookStrCharms=bookDefencePhy=[100, 100]

because all change if one of them changes.

Which is the best and easiest to define these variables?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
zjm1126
  • 34,604
  • 53
  • 121
  • 166
  • 1
    Are you defining characteristics for 6 different characters? And using the indexes 0 thru 5 to access a particular attribute for given character? Define a class and make these instance attributes! This is what OO was made for! – PaulMcG Mar 22 '11 at 03:40
  • possible duplicate of [How to clone or copy a list in Python?](http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list-in-python) – Artjom B. Apr 03 '15 at 15:18
  • @ArtjomB.: copying a list is a workaround. A valid solution for the OPs issue is to create a custom object like @Paul McGuire suggested and provide a way to enumerate its attributes e.g., `for name in self._skill_names: setattr(self, name, [100]*5)` – jfs Apr 05 '15 at 12:05

4 Answers4

5

Well, a step in the right direction would be to create a base list and then copy it using slice notation:

base = [100, 100, 100, 100]
value_a = base[:]
value_b = base[:]

and so on. This doesn't gain you much for the shorter lists, but it should be useful for the longer ones at least.

But I think more generally, a richer data structure would be better for something like this. Why not create a class? You could then use setattr to fill up class members in a fairly straightforward way.

class Weapons(object):
    def __init__(self, base):
        for weapon in ["saber", "sword", "axe"]:
            setattr(self, weapon, base[:])

w = Weapons([100, 100, 100])
print w.__dict__  

#output: {'sword': [100, 100, 100], 
#         'saber': [100, 100, 100], 
#         'axe': [100, 100, 100]}

w.axe[0] = 10
print w.axe       # output: [10, 100, 100]
print w.sword     # output: [100, 100, 100]
senderle
  • 145,869
  • 36
  • 209
  • 233
  • +1 for making a class to partition the data instead of having lots of global arrays. Assume there might be some structure to the arrays, but the OP did not give us any information that could help us there. – unholysampler Mar 22 '11 at 01:55
1

Define them all as empty arrays, then group the ones that need the same values into a list and iterate through that list, assigning the common values to each variable.

philosodad
  • 1,808
  • 14
  • 24
1

You could do something like:

defaultAttack = [100, 100,100, 100,100, 100]
accAttackSword = list(defaultAttack)
accAttackSaber = list(defaultAttack)

The list() constructor makes a copy of the list, so they will be able to change independently.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
1

You can use list multiplication

accAttackSword = [100]*6
....
bookWeight = [100]*2
....

You might consider grouping all of the variables with similar prefixes either into dictionaries or nested lists (EDIT - or classes/objects). This could have benefits later for organization, and would allow you to iterate thru and set them all to the same initial values.

bookVars = ['AttackPhy', 'AttackMag', 'StrInstrument', 'StrCharms']
bookValues = dict()
for i in bookVars:
    bookValues[i] = [100]*2

And to access...

bookValues
  {'AttackMag': [100, 100], 'StrCharms': [100, 100], 'StrInstrument': [100, 100], 'AttackPhy': [100, 100]}
bookValues['AttackMag']
  [100, 100]

EDIT - check out senderle's thing too. at a glance his seems a little better, but id definitely consider using one of our ideas - the point is to structure it a little more. whenever you have groups of variables with similar prefixed names, consider grouping them together in a more meaningful way. you are already doing so in your mind, so make the code follow!

jon_darkstar
  • 16,398
  • 7
  • 29
  • 37