将变量计数放入列表中

时间:2015-05-04 16:38:52

标签: python list

我有两个名单:

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

我是python的新手,而且我有一些裂缝,但我如何添加先前在我的游戏中使用变量的次数添加到option_frequency列表 - 匹配第一个清单的标题???

2 个答案:

答案 0 :(得分:4)

不完全确定您尝试做什么,但您是否只对记录关键字/操作的使用次数感兴趣?如果是这种情况,dict可能就是您要找的地方:

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])

打印

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

如果您只需要迭代代码字,可以使用dict.keys()

print option_dict.keys()

打印

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

答案 1 :(得分:0)

用户zip()

private final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("chat","I am in BroadCastReceiver");
         String msg = intent.getStringExtra("msg");
         Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
    }
};