Posts

Showing posts with the label python2

Python tips: reverse a dictionary

In Python, if you want to reverse a dictionary ( dict ), i.e. swap the keys and values, there are three methods that can help you achieve that goal depending on your needs: Method 1 : makes use of dict comprehension , and the dict must contain unique values Method 2 : makes use of dict.get() , and the dict doesn't contain unique values Method 3 : makes use of map(reversed, iter) , and you want the reverse dict to preserve the type and order of the original dict (if for example it is an OrderedDict ) I provided code implementations for each methods that work for Python 2.7.x and 3+, and the embedded code can be executed directly from the blog post through JDoodle , an online compiler and editor for many other languages (68 in total such as C/C++, Java, Go, SQL). I also compiled the average running times of the different methods in a table at the end ( Method 1 with dict comprehension is the fastest! Spoiler alert: Click to reveal winning method ) The code used ...

Installing OpenCV 3 on macOS with Homebrew

I am following this pyimagesearch tutorial to help me install OpenCV 3 on macOS Sierra 10.12.6 with Homebrew. I am using Python 2.7.15 & 3.6.4 installed with conda , instead of Python installed with Homebrew (like in the tutorial).  Contents    1. Major blog updates    2. Installation steps: Homebrew install command    3. cv2 import error    4.  Skipped brew tap    5.  --HEAD flag error    6.  Flags ignored: --with-contrib and --with-python3    7.  OpenCV installation test code    8.  SIFT and SURF test code Major blog updates May 29, 2018: added webcam test code + references, explained more the "sym-link" part, and added update of ~/.bash_profile Installation steps: Homebrew install command These were the prerequisites I had on my computer before installing OpenCV 3 via Homebrew: Operating system: macOS Sierra 10.12.6 Homebrew Two conda environm...