Skip to content
Advertisement

Splitting a list into N parts of approximately equal length

What is the best way to divide a list into roughly equal parts? For example, if the list has 7 elements and is split it into 2 parts, we want to get 3 elements in one part, and the other should have 4 elements. I’m looking for something like even_split(L, n) that breaks L into n parts. The code above

Split a string at newline characters

I have a string, say How do we split the above with the delimiter n (a newline)? The result should be Answer If you are concerned only with the trailing newline, you can do: See, str.lstrip() and str.strip() for variations. If you are more generally concerned by superfluous newlines producing empty items, you can do:

real time subprocess.Popen via stdout and PIPE

I am trying to grab stdout from a subprocess.Popen call and although I am achieving this easily by doing: I would like to grab stdout in “real time”. With the above method, PIPE is waiting to grab all the stdout and then it returns. So for logging purposes, this doesn’t meet my requirements (e.g. “see” what is going on while

How to read specific lines from a file (by line number)?

I’m using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this? Answer If the file to read is big, and you don’t want to read the whole file in memory at once: Note that i == n-1 for the nth line.

Fastest way to list all primes below N

This is the best algorithm I could come up. Can it be made even faster? This code has a flaw: Since numbers is an unordered set, there is no guarantee that numbers.pop() will remove the lowest number from the set. Nevertheless, it works (at least for me) for some input numbers: Answer Warning: timeit results may vary due to differences

ttk.Button returns None [duplicate]

This question already has answers here: Tkinter: AttributeError: NoneType object has no attribute <attribute name> (4 answers) Closed 8 months ago. I am trying to use the invoke method of a ttk.Button, as shown at TkDocs (look at “The Command Callback”), but I keep getting this error: AttributeError: ‘NoneType’ object has no attribute ‘invoke’ So, I tried this in the

How to access outer class from an inner class?

I have a situation like so… How can I access the Outer class’s method from the Inner class? Answer The methods of a nested class cannot directly access the instance attributes of the outer class. Note that it is not necessarily the case that an instance of the outer class exists even when you have created an instance of the

Advertisement