Skip to content

Tag: python

python: list assignment index out of range

i am getting this error on the last line: i dont understand how it can be out of range if it exists! please help Answer If row is a list, then don’t forget that deleting an element of a list will move all following elements back one place to fill the gap, so all of the later indices into the

Check if multiple strings exist in another string

How can I check if any of the strings in an array exists in another string? For example: How can I replace the if a in s: line to get the appropriate result? Answer You can use any: Similarly to check if all the strings from the list are found, use all instead of any.

Python: Regex to extract part of URL found between parentheses

I have this weirdly formatted URL. I have to extract the contents in ‘()’. Sample URL : http://sampleurl.com/(K(ThinkCode))/profile/view.aspx If I can extract ThinkCode out of it, I will be a happy man! I am having a tough time with regexing special chars like ‘(‘ and ‘/’. …

How to use `pytest` from Python?

I’m working in a project that recently switched to the pytest unittest framework. I was used to calling my tests from Eclipse, so that I can use the debugger (e.g. placing breakpoints to analyze how a test failure develops). Now this is no longer possible, since the only way to run the tests is via the …

Splicing NumPy arrays

I am having a problem splicing together two arrays. Let’s assume I have two arrays: When I do vstack((a,b)) I get and if I do hstack((a,b)) I get: But what I really want is: How do I accomplish this without using for loops (it needs to be fast)? Answer column_stack.

How to insert a character after every 2 characters in a string

Is there a pythonic way to insert an element into every 2nd element in a string? I have a string: ‘aabbccdd’ and I want the end result to be ‘aa-bb-cc-dd’. I am not sure how I would go about doing that. Answer Assume the string’s length is always an even number, The t can also be…

Numpy and line intersections

How would I use numpy to calculate the intersection between two line segments? In the code I have segment1 = ((x1,y1),(x2,y2)) and segment2 = ((x1,y1),(x2,y2)). Note segment1 does not equal segment2. So in my code I’ve also been calculating the slope and y-intercept, it would be nice if that could be av…