Reputation: 275
I have a list like below:
mylist = [((-1), (2)) ,((-3-4j), (5-6j)), ((-3+4j), (5+6j)), ((-7-8j), (-9-10j)), ((-7+8j), (-9+10j)), ((-11-12j), (13+14j)), ((-11+12j), (13+14j))]
I want to check two conditions on this list. For example, for the second element, ((-3-4j), (5-6j)), I want to check if the real part of 5-6j is positive and the sign of imaginary parts of (-3-4j) and (5-6j) is different then I want to know the number of that element in my list. The first element consists of two real numbers, and since 2 is positive, the first element meets the condition. As you can see, only the first element and the last two elements meet the conditions.
I have written a code based on things that I found but it only checks the real part of the second number of each element.
violates = [i for i, a in enumerate(mylist) if any([aa.real > 0 for aa in a])]
print ("violates=", violates)
The output, based on the conditions must be 0, 5 and 6. Thanks for any help.
Upvotes: 1
Views: 59
Reputation: 83527
Step back away from your code and attempt to describe the steps in more detail in words. For example, from what I understand in your question, you can start like this:
Notice how I have translated your description that you wrote as a paragraph and formatted it to look more like code. At the same time, I'm not overly worried about python syntax. I'm still using words, but trying to transform those words to be similar to the way I would write code in python. You need to finish fleshing out the details, especially where I have put "...". Then you will need to look at some documentation to figure out how to get the real and imaginary parts of a complex number and how to get the sign of a number.
Upvotes: 1
Reputation: 309
This works but 6 is not a valid case. Both imaginary parts have the same sign.
sign = lambda x: x and (1, -1)[x < 0]
violates = [(i,a) for i, a in enumerate(mylist) if (((a[0].real > 0) | (a[1].real > 0)) & ((sign(a[0].imag) + sign(a[1].imag))==0))]
Upvotes: 0