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
Tag: python
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 …
Product code looks like abcd2343, how to split by letters and numbers?
I have a list of product codes in a text file, on each line is the product code that looks like: abcd2343 abw34324 abc3243-23A So it is letters followed by numbers and other characters. I want to split on the first occurrence of a number. Answer Or, if you want to split on the first occurrence of a digit: d+
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…
How find values in an array that meet two conditions using Python
I have an array and I want to find the indices of the element s that meet two conditions i.e. I can use numpy.nonzero(a>3) or numpy.nonzero(a<8) but not numpy.nonzero(a>3 and a<8) which gives the error: When I try to use any or all I get the same error. Is it possible to combine two conditional te…
Percentage chance to make action
Simple problem: How can I write some_function, or an expression involving percentage_chance, in order to solve this problem? Answer You could use random.random: