Python keyword “in” and Its Different Efficiency with lists and dictionaries

The efficiency of Python keyword “in” may depend on the data structure it is applied to. In this post I will show you the efficiency difference of “in” when used with lists and dictionaries (hash tables).

First, I composed the following two python scripts to test the elapsed time when using lists and dictionaries.

The script for list creates a list of range(10000) and check for 0-9999 whether they are in the list using keyword “in”.


import time

in_list = range(10000)
counter = 0
start = time.clock()
for i in range(10000):
    if i in in_list:
        counter += 1
end = time.clock()
print "hit number:"
print counter
print "elapsed time:"
print (end-start)

The script for dictionary first creates a dictionary consisting 10000 key:value pairs. Then it tests the time needed to check whether 0-9999 are in the dictionary.


import time

in_dict = {k:v for k,v in zip(range(10000),range(10000))}
counter = 0
start = time.clock()
for i in range(10000):
    if i in in_dict:
        counter += 1
end = time.clock()
print "hit number:"
print counter
print "elapsed time:"
print (end-start)

 

The results are as follow, respectively.

list:

hit number:
10000
elapsed time:
0.694395

dictionary:

hit number:
10000
elapsed time:
0.001719

The results show that although the keyword is “in” for both of the data structures, the efficiency varies. In another word, the efficiency of Python keyword “in” depends on the data structure it is applied to.

 

An Example Showing the Difference Between [ and [[ in shell (bash) on pathname Expansion

How to understand “No filename expansion or word splitting takes place between [[ and ]], but there is parameter expansion and command substitution.” [1], especially the “no filename expansion” part?

“no filename expansion” means that wildcards such as “*”, “?” and “[” will not be treated specially. That is, a string like “/dev/stder?” will not be expanded to match string patterns which is started with “/dev/stder” and has one character we are not certain of.

A simple example showing the difference between [ and [[ for filename expansion is as follows.

if [[ -e /dev/stder? ]]; then echo "matched"; else echo "wrong"; fi

will return

wrong

whereas

if [ -e /dev/stder? ]; then echo "matched"; else echo "wrong"; fi

will yield

matched

 

[1] Cooper, M. (2014). Advanced Bash Scripting Guide. Рипол Классик.

How to Install gulp and bower globally on Mac OSX

This post illustrates the procedure of installing gulp and bower, two node modules, on Mac OSX.

First, assuming you’ve already installed Homebrew on Mac, search for the correct version of node.

brew search node

The version currently recommended by nodejs organization is 4.4.7-lts. So that here we install this version.

brew install homebrew/versions/node4-lts

Then we can test whether the installation is successful by viewing the versions of node and npm.

node -v

npm -v

Here comes a trick before installing node modules gulp and bower. We have to change npm configuration as follows.

npm config set prefix /usr/local

After the above step, we can install gulp and bower respectively using the commands below.

npm install gulp -g

npm install bower -g

Now gulp and bower have already been successfully installed on your Mac. You can test by the following commands.

gulp -v

bower -v

 

How to Install srilm for Kaldi on RedHat / Ubuntu

Current versions of Kaldi do not install srilm as a default tool.

While trying to install srilm on RedHat / Ubuntu, I encountered some problems. Here are the tricks for these problems.

  1. Simply running /tools/extras/install_srilm.sh cannot install srilm automatically.
  2. The right procedure is as follows.
    1. Download srilm package from http://www.speech.sri.com/projects/srilm/download.html.
    2. Rename the package as “srilm.tgz”.
    3. Copy the package to /tools.
    4. Run /tools/extras/install_srilm.sh.
    5. Source /tools/env.sh.
  3. For a safer option, you may add the content in /tools/env.sh into ~/.bash_profile.

 

MATLAB GUI

This is a brief note of the programming on MATLAB GUI.

As part of a DSP project, the programming of GUI was on my duty in my third year studying in USTC. To offer some tips for novices, I composed this short passage about GUI programming.

Firstly, the GUI coding seems to be much easier in MATLAB since we can simply draw the graph of buttons rather than writing all the properties of the objects by ourselves. To begin your journey in the GUI programming, you can simply click on “new”-“GUI”. Secondly, the rudimentary of MATLAB GUI includes the term handle, especially the handle of figures. But don’t worry if you can’t catch the precise meaning of it, after some practice you may have the sufficient understanding of GUI to solve some simple problems in your everyday study, just the same as I am right now. Thirdly, let us talk about the most essential part of GUI coding. That is the communication between objects in a GUI window and between two GUI windows (which can easily extend to the data sharing among more windows). I know that you may have some books right in your hands and may have already know that there are at least seven methods to accomplish the interaction between different windows. What I am going to say are the exact methods I used, which I think might be the easiest method. With respect to the interaction between objects in the same window, you can use a struct in your function named “openfcn”. Since the functions below will share the struct (which can be easily understood by the input of the functions of the buttons), we can simply use the struct to deposit and withdraw our data. Then, as for the data communication between GUI windows, use the function “findall”. You may also need to percept the property “visible” to change the visibility of GUI windows. For more information, you can mail peidong at mail.ustc.edu.cn.