2017-05-05 9 views
-1

のPython 3.6印刷辞書を引い二つの要素

すべてのデバッグ出力がPyCharmからである2017年1月2日

私は、コードのこの部分に到達するプログラムがあります。

if len(errdict) == 21: 
    for k, v in errdict.items(): 
     if k == 'packets output' or 'bytes': 
      continue 
     print(k, v) 
    print() 

値のを

k={str}'input errors' 

__len__ = {int} 21 
'CRC' (73390624) = {int} 0 
'babbles' (73390464) = {int} 0 
'bytes' (73390496) = {int} 0 
'collisions' (73455360) = {int} 0 
'deferred' (73455440) = {int} 0 
'frame' (73390592) = {int} 0 
'ignored' (73390688) = {int} 0 
'input errors' (73455280) = {int} 0 
'input packets with dribble condition detected' (63021088) = {int} 0 
'interface resets' (73451808) = {int} 0 
'late collision' (73455400) = {int} 0 
'lost carrier' (73455520) = {int} 0 
'no carrier' (73455480) = {int} 0 
'output buffer failures' (73451856) = {int} 0 
'output buffers swapped out' (73055328) = {int} 0 
'output errors' (73455120) = {int} 0 
'overrun' (73390112) = {int} 0 
'packets output' (73455320) = {int} 0 
'underruns' (73455080) = {int} 0 
'unknown protocol drops' (73451904) = {int} 0 
'watchdog' (73455160) = {int} 0 

この2つの行を削除すると次のようになります。 :

 if k == 'packets output' or 'bytes': 
      continue 

辞書のすべてのキーと値のペアをすべて正しく出力します。私はすべての辞書がキーとして 'パケット出力'または 'バイト'を持つ2つのキー\値の組を除いて表示されるようにしたい。

これらの2行では、すべてのキーと値のペアがスキップされ、何も印刷されません。私は単に理由を見ません。 '入力エラー'が自分の条件と一致しないため、続行をスキップして印刷する必要があります。一致する2つのキーを除いて行を下に移動するとスキップする必要があります。

私には何が欠けていますか?

ありがとうございます。

+1

'。 ..またはk == 'bytes'' –

+1

'' k =='パケット出力 'またはk ==' bytes 'の場合: ' –

+0

Nick A、Maurice Meyer、wegrataに - ありがとう。それは私が逃したものです。私はちょうど修正プログラムを使用してプログラムを実行し、正しく動作しています。 – MarkS

答えて

7
if k == 'packets output' or 'bytes' 

これは常に'bytes'としてtrueと評価されますがtruthy値であり、あなたは両方にkを比較する必要があります。

pythonically
if k == 'packets output' or k == 'bytes' 

以上:

if k in ['packets output', 'bytes'] 
+0

OPが多くの 'k'sやより大きな値のグループのメンバをテストしているならば、' set'を使う方が良いでしょう。 – blacksite

0
if len(errdict) == 21: 
    for k, v in errdict.items(): 
     if k == 'packets output' or k == 'bytes': 
      continue 
     print(k, v) 
    print() 
+1

答えがOPの問題を解決する理由を実際に説明しておけば、助けになります。 – kdopen