GH K23
GH K23

Reputation: 11

putting variable count into a list

I've got two lists:

    option_title = ['attack', 'defend', 'coattack', 'codefend']
    option_frequency = [0, 0, 0, 0]

I'm a newbie to python and I've had a few cracks at it, but how do I add the number of times a variable was used previously in my game to add to the option_frequency list - matching the titles of the first list???

Upvotes: 0

Views: 69

Answers (2)

EvenLisle
EvenLisle

Reputation: 4812

Not entirely sure what you're attempting to do, but are you only interested in recording the number of times the keywords/actions are used? If that's the case, a dict may be what you're looking for:

option_dict = {"attack": 0, "defend": 0, "coattack": 0, "codefend": 0}
option_dict["attack"] += 1
print "\n".join([key + " * " + str(option_dict[key]) for key in option_dict])

prints

codefend * 0
attack * 1
defend * 0
coattack * 0

If you should need to iterate over the codewords only, you can use dict.keys():

print option_dict.keys()

which prints

['codefend', 'attack', 'defend', 'coattack']

Upvotes: 4

Bun S.
Bun S.

Reputation: 111

User zip() :

>>> zip(option_title, option_frequency)
>>> [('attack', 0), ('defend', 0), ('coattack', 0), ('codefend', 0)]

Upvotes: 0

Related Questions