Skip to content

Tag: python

A recursive function to sort a list of ints

I want to define a recursive function can sort any list of ints: Calling this function on a list [3, 1, 2,4,7,5,6,9,8] should give me: But I get: Please help me to fix the problem, actual code would be appreciated. Thanks! Answer The quick sort is recursive and easy to implement in Python: will give:

Javascript to Django views.py?

This may sound simple, but how do I send the data from a Javascript array in my index.html template to my views.py? When the user clicks a “Recommend” button, my code calls a function that accesses my database and prints a name on the template. I have an array of checkbox values in Javascript that…

Python imports relative path

I’ve got a project where I would like to use some python classes located in other directories. Example structure: The absolute path varies, because this project is run on different machines. When my python file with MySampleClass located in /mydir is executed, how do I import OtherClassRoot located in /…

Is there a faster version of numpy.random.shuffle?

I’m using numpy.random.shuffle in order to compute a statistic on randomized columns of a 2D array. The Python code is as follows: The speed I get is something like this: 1 loops, best of 3: 391 ms per loop I tried to Cythonize this function but I wasn’t sure how to replace the call to np.random.s…

pygame.error: video system not initialized

I am getting this error whenever I attempt to execute my pygame code: pygame.error: video system not initialized Answer You haven’t called pygame.init() anywhere. See the basic Intro tutorial, or the specific Import and Initialize tutorial, which explains: Before you can do much with pygame, you will ne…

python urllib2 and unicode

I would like to collect information from the results given by a search engine. But I can only write text instead of unicode in the query part. give this error Answer Encode the Unicode data to UTF-8, then URL-encode: Demo: Using urllib.urlencode() to build the parameters is easier, but you can also just escap…

How to get PID by process name?

Is there any way I can get the PID by process name in Python? For example I need to get 3110 by chrome. Answer You can get the pid of processes by name using pidof through subprocess.check_output: check_output([“pidof”,name]) will run the command as “pidof process_name”, If the return …

Align matplotlib scatter marker left and or right

I am using the matplotlib scatterplot function to create the appearance of handles on vertical lines to delineate certain parts of a graph. However, in order to make them look correct, I need to be able to align the scatter plot marker to the left (for the left line / delineator) and / or right (for the right…