2016-08-14 62 views
0

我有下面的代码,并试图迭代每行的结果,并检查'​​未经训练'字典中的计算值是否大于50%。然而,一些行是NoneType,我得到错误:TypeError:'NoneType'oject不是可以下载的。有没有一种方法可以让我清楚这一点,并且仍然可以迭代得到我想要的结果?Python迭代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%)   
+0

你是什么意思'''总=受训[T]'''是什么意思?当你为训练过的t做循环时,''','''t'''已经包含你需要的元素,而不是元素的索引。 –

+1

@IgorPomaranskiy遍历一个'dict'返回它的键...所以'训练[t]'将访问它的值... –

+0

@JonClements对不起,你是对的,我的坏! –

回答

1

你不能只跳过线,其中untrainedNone,或者您将跳过trained值了。相反,你应该直接添加附加的保护到if条件检查百分比是否> 50:

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

这就是它!没想到要添加额外的'和'声明。我希望宇宙奖励你和今天帮助我的一切。非常感谢你! – MBasith

4

跳过无价值观

if untrained is None: 
    continue 
+0

或者在更多Pythonic版本中,'如果不是未经训练:' – DeepSpace

+4

@DeepSpace我宁可不推荐使用这种方法。有时它是可靠的,但有时不是。 '''如果不是未经训练的'''不仅会跳过'''None',而且跳过'''0''和空的数据结构(列表,元组,集合等),这可能是不需要,有时很难找出结果不正确的原因。所以最好做一下明确的比较:'''如果未经训练的不是None:'''。 –

+0

@Akilesh我加了建议的代码,但现在我错过了第一列(训练)的一些值。我粘贴上面的修改后的代码和结果。你能检查我做错了什么吗? – MBasith

相关问题