How-to articles, tricks, and solutions about PYTHON
To print the full NumPy array without truncation, you can use the numpy.set_printoptions() function and set the threshold parameter to np.inf.
You can use the items() method to print the key-value pairs of a dictionary in Python.
To print to stderr in Python, you can use the stderr attribute of the sys module:
There are several ways to profile a Python script:
In Python, you can use the format() method or f-strings (available in Python 3.6 and above) to interpolate a variable's value into a string.
You can use the numpy.genfromtxt() function to read CSV data into a NumPy record array.
To remove a substring from the end of a string, you can use the rsplit() method and specify the substring as the delimiter.
There are several ways to remove duplicates from a list while preserving order.
You can use the list method pop(index) to remove the first item in a list.
You can use the shutil module in Python to remove a folder that is not empty.
There are a few ways to reverse a list in Python, depending on what you want to do with the reversed list.
To reverse a string in Python, you can use the following method:
You can use the DataFrame.loc method to select rows from a DataFrame based on column values.
In matplotlib, you can set the font size of the figure title and axes labels using the pyplot module.
In Python, dictionaries are unordered collections of key-value pairs.
You can use the sorted() function and pass it the dictionary, along with the key parameter set to a lambda function that returns the value from the dictionary.
To sort a list of dictionaries by a value of the dictionary, you can use the sorted() function and specify the key parameter to be a lambda function that returns the value you want to sort by.
Here is a function that takes a list and an integer n and splits the list into n equally sized chunks:
In Python, you can use the built-in list() function to convert a string into a list of characters.
Here is an example of how to split a string into a list of words in Python:
You can split a string over multiple lines by using triple quotes, either ''' or """ .
In Python, you can terminate a script by using the exit() function from the sys module.
There are a few ways to trim whitespace from a string in Python.
In Python, you can use the .strip() method to remove leading and trailing whitespace from a string.