Python迭代NoneType

时间:2016-08-14 13:16:41

标签: python dictionary nonetype

我有以下代码,并且我试图迭代每行的结果并检查未经训练的'中的计算值。字典大于50%。但是,有些行是NoneType,我收到错误:TypeError:' NoneType'对象不可订阅。有没有办法我可以卵形这个并仍然迭代以获得我想要的输出?

from collections import namedtuple
from itertools import zip_longest

trained = {'Dog': 4, 'Cat': 3, 'Bird': 1, 'Fish': 12, 'Mouse': 19, 'Frog': 6}
untrained = {'Cat': 6, 'Mouse': 7, 'Dog': 3, 'Wolf': 9}

Score = namedtuple('Score', ('total', 'percent', 'name'))

trained_scores = []
for t in trained:
    trained_scores.append(
        Score(total=trained[t],
              percent=(trained[t]/(trained[t]+untrained.get(t, 0)))*100,
              name=t)
    )

untrained_scores = []
for t in untrained:
    untrained_scores.append(
        Score(total=untrained[t],
              percent=(untrained[t]/(untrained[t]+trained.get(t, 0)))*100,
              name=t)
    )

# trained_scores.sort(reverse=True)
# untrained_scores.sort(reverse=True)

row_template = '{:<30} {:<30}'
item_template = '{0.name:<10} {0.total:>3} ({0.percent:>6.2f}%)'
print('='*85)
print(row_template.format('Trained', 'Untrained'))
print('='*85)

for trained, untrained in zip_longest(trained_scores, untrained_scores):
    x = row_template.format(
        '' if trained is None else item_template.format(trained),
        '' if untrained is None else item_template.format(untrained)
    )
    print(x)

当前输出:

=====================================================================================
Trained                        Untrained                     
=====================================================================================
Mouse       19 ( 73.08%)       Mouse        7 ( 26.92%)      
Cat          3 ( 33.33%)       Cat          6 ( 66.67%)      
Frog         6 (100.00%)       Wolf         9 (100.00%)      
Dog          4 ( 57.14%)       Dog          3 ( 42.86%)      
Bird         1 (100.00%)                                     
Fish        12 (100.00%) 

期望的输出:

=====================================================================================
Trained                        Untrained                     
=====================================================================================
Mouse       19 ( 73.08%)       Mouse        7 ( 26.92%)       
Cat          3 ( 33.33%)       Cat          6 ( 66.67%)  <-- Above 50%    
Frog         6 (100.00%)       Wolf         9 (100.00%)  <-- Above 50%      
Dog          4 ( 57.14%)       Dog          3 ( 42.86%)      
Bird         1 (100.00%)                                     
Fish        12 (100.00%)                                     

更新:

更新了有效的建议代码。谢谢你的帮助!

if untrained is not None and untrained[1] > 50:
    print(x + '<-- Above 50%')
else:
    print(x)

结果:

=====================================================================================
Trained                        Untrained                     
=====================================================================================
Mouse       19 ( 73.08%)       Wolf         9 (100.00%)       <-- Above 50%
Fish        12 (100.00%)       Mouse        7 ( 26.92%)      
Frog         6 (100.00%)       Cat          6 ( 66.67%)       <-- Above 50%
Dog          4 ( 57.14%)       Dog          3 ( 42.86%)      
Cat          3 ( 33.33%)                                     
Bird         1 (100.00%)        

2 个答案:

答案 0 :(得分:4)

跳过无值

if untrained is None:
    continue

答案 1 :(得分:1)

您不能只跳过untrainedNone的行,或者您也会跳过trained值。相反,您应该直接在if条件中添加一个额外的警卫,检查百分比是否为&gt; 50:

if untrained is not None and untrained[1] > 50:
    print(x + '<-- Above 50%')
else:
    print(x)