简体   繁体   English

Python遍历NoneType

[英]Python Iterate over NoneType

I have the following code and am attempting to iterate over the results of each line and check if a calculated value in the 'untrained' dictionary is greater than 50%. 我有以下代码,并尝试遍历每行的结果,并检查“未训练”字典中的计算值是否大于50%。 However, some of the lines are NoneType and I am getting error: TypeError: 'NoneType' oject is not subscriptable. 但是,有些行是NoneType,并且出现错误:TypeError:'NoneType'对象不可下标。 Is there a way I can ovoid this and still iterate to get my desired output below? 有没有一种方法可以避免这种情况,并且仍然反复进行下面的操作以得到所需的输出?

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)

Current Output: 电流输出:

=====================================================================================
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%) 

Desired Output: 所需输出:

=====================================================================================
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%)                                     

Update!: 更新!:

Updated with the suggested code that works. 更新了建议的有效代码。 Thanks for all the help! 感谢您的所有帮助!

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

Result: 结果:

=====================================================================================
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%)        

Skip over None values 跳过无值

if untrained is None:
    continue

You can not just skip the lines where untrained is None or you will skip the trained values, too. 您不仅可以跳过untrained为“ None的行,还可以跳过trained值。 Instead, you should add an additional guard directly to the if condition checking whether the percentage is > 50: 相反,您应该直接在if条件中添加一个额外的防护措施,以检查百分比是否大于50:

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

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

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