Skip to content

Tag: python

Circular shift of vector (equivalent to numpy.roll)

I have a vector: And I’d like to do something like: Is there a function like that in R? I’ve been googling around, but “R Roll” mostly gives me pages about Spanish pronunciation. Answer How about using head and tail… One cool thing about using head and tail… you get a rever…

Mergesort with Python

I couldn’t find any working Python 3.3 mergesort algorithm codes, so I made one myself. Is there any way to speed it up? It sorts 20,000 numbers in about 0.3-0.5 seconds Answer You can initialise the whole result list in the top level call to mergesort: Then for the recursive calls you can use a helper …

Handle Multiple Checkboxes with a Single Serverside Variable

I have the following HTML code: And then ideally I would like to have the following python serverside code: I tried doing this by making the HTML sports_played name and array, sports_played[], but that didn’t do anything and right now it just always returns the first selected item. Is this possible? Rea…

Remove Last instance of a character and rest of a string

If I have a string as follows: foo_bar_one_two_three Is there a clean way, with RegEx, to return: foo_bar_one_two? I know I can use split, pop and join for this, but I’m looking for a cleaner solution. Answer Which behaves like this: See in the documentation entry for str.rsplit([sep[, maxsplit]]).

GeoDjango GEOSException error

Trying to install a GeoDjango on my machine. I’m really new to Python and being brought into a project that has been a very tricky install for the other team members. I installed Python 2.7 and GEOS using brew, and running PSQL 9.2.4 but keep getting this error when I try to get the webserver running: C…

Python3 Convert all characters to HTML Entities

I’m using Python3 and I wonder if there is a module or a default function for converting all characters of a text to html entities (even the letters and digits) because I don’t want to make a translation map for this. Solved: As @justhalf told me, I found the solution by making this function: Answ…

watchdog monitoring file for changes

I have a need to watch a log file for changes. After looking through stackoverflow questions, I see people recommending watchdog. So I’m trying to test, and am not sure where to add the code for when files change: Where do I add the “got it” — in the while loop if the files have been added/c…

Intersecting two dictionaries

I am working on a search program over an inverted index. The index itself is a dictionary whose keys are terms and whose values are themselves dictionaries of short documents, with ID numbers as keys and their text content as values. To perform an ‘AND’ search for two terms, I thus need to interse…