Detect alternating signs
Is there a nice and short way to tell whether a python list (or numpy array) contains numbers with alternating signs? In other words:
is_alternating_signs([1, -1, 1, -1, 1]) == True
is_alternating_signs([-1, 1, -1, 1, -1]) == True
is_alternating_signs([1, -1, 1, -1, -1]开发者_开发技巧) == False
OK, thanks to SO "related" feature. I found this question and adopted the answer by ianalis and the comment by lazyr
def is_alternating_signs(a):
return numpy.all(numpy.abs(numpy.diff(numpy.sign(a))) == 2)
print is_alternating_signs([1, -1, 1, -1, 1])
print is_alternating_signs([-1, 1, -1, 1, -1])
print is_alternating_signs([1, -1, 1, -1, -1])
The output is
True
True
False
You could check every even member is negative and every odd member is positive by taking a slice of every second item, starting at either the beginning or from position one. Also test the reverse to cover both possibilities.
so:
def is_alternating_signs(l):
return ( (all(x<0 for x in l[::2]) and all(x>=0 for x in l[1::2])) or
(all(x>=0 for x in l[::2]) and all(x<0 for x in l[1::2])))
Using decimal module and is_signed method:
from decimal import Decimal
a = [1, -1, 1, -1, 1]
b = [-1, 1, -1, 1, -1]
c = [1, -1, 1, -1, -1]
def is_alternating_signs(values):
lVals = [Decimal(val).is_signed() for val in values]
prevVal = lVals.pop(0)
for val in lVals:
if prevVal == val:
return False
prevVal = val
return True
is_alternating_signs(a)
is_alternating_signs(b)
is_alternating_signs(c)
I like pairwise:
from itertools import izip, tee
def pairwise(iterable):
a, b = tee(iterable)
next(b)
return izip(a, b)
def is_alternating_signs(iterable):
return all(x < 0 < y or x > 0 > y for x, y in pairwise(iterable))
If there are no zeros in iterable
this also works:
def is_alternating_signs(iterable):
return all((x < 0) == (0 < y) for x, y in pairwise(iterable))
how about something like...
def is_alternating_signs(aList):
return all( (aList[i]^aList[i-1])<0 for i in range(1,len(aList)) )
What about just the straight-forward solution by looping through it once and testing? Quite possibly the fastest, too, because many of the other solutions loop through the list multiple times.
def signs_are_alternating(numbers):
"""Return True if numbers in given list have alternating signs, False
otherwise. If given list has less than 2 elements, return False.
>>> signs_are_alternating([1, -1, 1, -1, 1])
True
>>> signs_are_alternating([-1, 1, -1, 1, -1])
True
>>> signs_are_alternating([1, -1, 1, -1, -1])
False
"""
if len(numbers) < 2:
return False
previous_positive = (numbers[0] < 0) # Pretend it starts alternating
for number in numbers:
this_positive = (number >= 0)
if previous_positive == this_positive:
return False
previous_positive = this_positive
return True
Note that I wasn't quite sure what the intended behaviour is if the input list has less than 2 elements.
Here's my one-liner, which is probably less efficient than some of the other suggestions:
def is_alternating_signs(lst):
return all(x * y < 0 for x, y in zip(lst, lst[1:]))
精彩评论