I created a dictionary with the following syntax
我用以下语法创建了一个字典
frequency_m= dict(zip(unique, counts))
which results into:
结果如下:
{0: 3512488, 1: 2606, 2: 3553, 3: 3929, ..........}
I want to classify the key, value pairs as binary - '1' or '0'. Below I represented
我想将键值对分类为二进制 - '1'或'0'。我代表的下面
for k, v in frequency_m.iteritems():
if k ==0:
print '0', v
else:
print '1', sum(v)
obviously that generates TypeError: 'numpy.int64' object is not iterable
. I am sure I need to iterate over the values and sum that up for the values other than '0'. I am not getting it. Any thoughts?
显然会生成TypeError:'numpy.int64'对象不可迭代。我确信我需要迭代这些值并将其与“0”以外的值相加。我没有得到它。有什么想法吗?
0 3512488
1 2606
1 3553
1 3929
my goal here is to output the table as
我的目标是输出表格
0 3512488
1 10088
I tried following as well: ** np.sum((value for key, value in frequency_m.iteritems() if key != '0'))**
, it sums up all the values and does not yield my goal.
我也尝试过跟随:** np.sum((键的值,frequency_m.iteritems()中的值,如果键!='0'))**,它总结了所有的值并且不会产生我的目标。
1 个解决方案
#1
1
Just change your comprehension to check for 0
instead of '0'
:
只需改变你的理解力来检查0而不是'0':
np.sum((value for key, value in frequency_m.iteritems() if key != 0))
#1
1
Just change your comprehension to check for 0
instead of '0'
:
只需改变你的理解力来检查0而不是'0':
np.sum((value for key, value in frequency_m.iteritems() if key != 0))