Skip to content

Tag: python

How can I loop through blocks of lines in a file?

I have a text file that looks like this, with blocks of lines separated by blank lines: How can I loop through the blocks and process the data in each block? eventually I want to gather the name, family name and age values into three columns, like so: Answer Here’s another way, using itertools.groupby. …

How to change the font size on a matplotlib plot

How does one change the font size for all elements (ticks, labels, title) on a matplotlib plot? I know how to change the tick label sizes, this is done with: But how does one change the rest? Answer From the matplotlib documentation, This sets the font of all items to the font specified by the kwargs object, …

Can you have variables within triple quotes? If so, how?

This is probably a very simple question for some, but it has me stumped. Can you use variables within python’s triple-quotes? In the following example, how do use variables in the text: I would like it to result in: If not what is the best way to handle large chunks of text where you need a couple varia…

Call Class Method From Another Class

Is there a way to call the method of a class from another class? I am looking for something like PHP’s call_user_func_array(). Here is what I want to happen: Answer update: Just saw the reference to call_user_func_array in your post. that’s different. use getattr to get the function object and the…

Efficiently detect sign-changes in python

I want to do exactly what this guy did: Python – count sign changes However I need to optimize it to run super fast. In brief I want to take a time series and tell every time it crosses crosses zero (changes sign). I want to record the time in between zero crossings. Since this is real data (32 bit

Appending turns my list to NoneType [duplicate]

This question already has answers here: Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list? (6 answers) Closed 5 months ago. In Python Shell, I entered: and got but when I tried: and got Does anyone know what’s going on? …