简体   繁体   English

将变量计数放入列表

[英]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??? 我是python的新手,但我遇到了一些麻烦,但是如何增加以前在游戏中使用变量添加到option_frequency列表的次数-匹配第一个列表的标题? ??

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() : 如果只需要遍历代码字,则可以使用dict.keys()

print option_dict.keys()

which prints 哪个打印

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

User zip() : 用户zip()

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

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

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