Skip to content

Tag: python

Finding a key recursively in a dictionary

I’m trying to write a very simple function to recursively search through a possibly nested (in the most extreme cases ten levels deep) Python dictionary and return the first value it finds from the given key. I cannot understand why my code doesn’t work for nested dictionaries. It returns None. It…

Sum the digits of a number

If I want to find the sum of the digits of a number, i.e.: Input: 932 Output: 14, which is (9 + 3 + 2) What is the fastest way of doing this? I instinctively did: and I found this online: Which is best to use for speed, and are there any other methods which are even faster? Answer Both

change the colorbar width

I have created this, as you can see the colorbar is very thin, is there anyway of increasing the width of the colorbar without increasing the height Answer You need to get hold of the axes of the colorbar. Assuming you hold a reference to the colorbar in cbar you get to the axes via cbar.ax. You can control t…

Kill program listening to a given port

Before the main code execution starts, I want to check if a particular port is open or not.. and if it is.. then close the port so that code can start “cleanly”. So I google online and found the code here: http://www.paulwhippconsulting.com.au/tips/63-finding-and-killing-processes-on-ports But jus…