array with only one of same value python code example
Example 1: how to sum only the even values in python
>>> myList = [1, 3, 5, 6, 8, 10, 34, 2, 0, 3]
>>> result = 0
>>> for i in myList:
... if not i % 2:
... result += i
...
>>> print(result)
60
>>>
Example 2: cluster on lists of values that start with a certain value
def grouper(iterable):
prev = None
group = []
for item in iterable:
if not prev or item - prev <= 15:
group.append(item)
else:
yield group
group = [item]
prev = item
if group:
yield group
numbers = [123, 124, 128, 160, 167, 213, 215, 230, 245, 255, 257, 400, 401, 402, 430]
dict(enumerate(grouper(numbers), 1))