print dictionary minus two elements

2024/11/13 9:32:23

Python 3.6

All debug output is from PyCharm 2017.1.2

I have a program that gets to this portion of the code:

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

The values of k: and errdict{} at the time of execution are as follows:

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

If I remove these two lines:

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

it correctly prints out all 21 key\value pairs of the dictionary. I want all of the dictionary to print EXCEPT the two key\value pairs that have 'packets output' or 'bytes' as the key.

With those two lines every key\value pair is skipped over and nothing prints. I simply do not see why. 'input errors' does not match my condition, so the continue should be skipped and it should print, and so on down the line except for the two keys that do match and they should be skipped.

What am I missing?

Thank you.

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

This will always evaluate to true as 'bytes' is a truthy value, you need to compare k to both:

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

Or more pythonically:

if k in ['packets output', 'bytes']
https://en.xdnf.cn/q/119377.html

Related Q&A

Write CSV file using Python with the help of a csv dictionary / nested csv dictionary

I am having a csv file and i want to write it to another csv file. Its a bit complicated than it seems. Hoping someone to correct my code and rewrite it, so that i can get the desired csvfile. I am usi…

saving data to txt file using python

I am new in python, and I really need some help. I am doing this memory game where I need to save user, game score and time into a text file using python. I have tried several ways to do it, but nothin…

How can I create bounding boxes/contour around the outer object only - Python OpenCV

So Ive been trying to make bounding boxes around a couple of fruits that I made in paint. Im a total beginner to opencv so I watched a couple tutorials and the code that I typed made, makes contours ar…

resuming download file ftp python3.*

There is a file (1-7Gb) that you need to pick up. The network periodically falls, so it is necessary to implement the method of resume. For example, in 1 communication session downloaded 20% the networ…

printing files based on character

I have a directory(data) that contain thousand of files.Each time I want to select three files that are just differ by only one characterAB[C,D,E] and want to perform some computation on the selected t…

Parsing CSV file using Panda

I have been using matplotlib for quite some time now and it is great however, I want to switch to panda and my first attempt at it didnt go so well.My data set looks like this:sam,123,184,2.6,543 winte…

Getting division by zero error with Python and OpenCV

I am using this code to remove the lines from the following image:I dont know the reason, but it gives me as output ZeroDivisionError: division by zero error on line 34 - x0, x1, y0, y1 = (0, im_wb.sha…

Pandas complex calculation based on other columns

I have successfully created new columns based on arithmetic for other columns but now I have a more challenging need to first select elements based on matches of multiple columns then perform math and …

how to generate word from a to z [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to webscrape all shoes on nike page using python

I am trying to webscrape all the shoes on https://www.nike.com/w/mens-shoes-nik1zy7ok. How do I scrape all the shoes including the shoes that load as you scroll down the page? The exact information I …