How to check whether a sentence is valid in Python? Examples: Answer Check out NLTK. They have support for grammars that you can use to parse your sentence. You can define a grammar, or use one that is provided, along with a context-free parser. If the sentence parses, then it has valid grammar; if not, then …
Tag: python
Flask POST request is causing server to crash
I am trying to make a simple api in Flask, the first step being getting the POST json data. (I just want to print it for now) This is my code and when I request /api with json data, it returns a 500 error. Any thoughts on why this is happening? The curl command: body.json: Answer First what you want
how to create empty c type array (or just one NULL value) in python?
I was wondering whether (and how) one can create empty c type arrays in python. The following code works fine if I want to initialize an array with zeros: If data is a list of None values however, I get an error message: is there a way to initialize and array with NONEs or NULLs? EDIT 1: the reason why
Check for camel case in Python
I would like to check if a string is a camel case or not (boolean). I am inclined to use a regex but any other elegant solution would work. I wrote a simple regex Would this be correct? Or am I missing something? Edit I would like to capture names in a collection of text documents of the format Edit2
How to close IPython Notebook properly?
How to close IPython Notebook properly? Currently, I just close the browser tabs and then use Ctrl+C in the terminal. Unfortunately, neither exit() nor ticking Kill kernel upon exit does help (they do kill the kernel they but don’t exit the iPython). Answer There isn’t currently a better way to do…
How do I index the 3 highest values in a list?
so i have these 2 lists: I have to find the 3 highest scores and tally these scores with their respective scorers in the name list, so that I can have new lists I’m thinking of indexing the highest scores, appending them into a list than using the index to tally these scores with the names. my question …
Extract a part of the filepath (a directory) in Python
I need to extract the name of the parent directory of a certain path. This is what it looks like: I would like to extract directory_i_need. Answer And you can continue doing this as many times as necessary… Edit: from os.path, you can use either os.path.split or os.path.basename:
Python itertools.product with variable number of arguments
I am trying to write a module to combine a variable number of lists using itertools.product. The closest I can get is: This doesn’t work, because lists is a single list, so it just returns the original sequence. But I can’t figure out how to pass each element of the lists variable to itertools. Th…
python RuntimeError: dictionary changed size during iteration
I have obj like this It should be expand to I wrote code below, splite by ‘.’, remove old key, append new key if contains ‘.’, but it said RuntimeError: dictionary changed size during iteration for k, v in obj.iteritems(): RuntimeError: dictionary changed size during iteration Answer L…
Map Reduce on timestamps
Mongodb Database: How can I Map-Reduce or aggregate on the above data to generate an output as: I am trying to write some code but its difficult to average on unsorted dates. Are there any built in functions averaging on time to do this? Answer You could do min/max on the dates like described here for numbers…