How’s that?
from itertools import groupby
input_list = [0, 1, 0, 0, 1, 1, 1, 0, 1, 0]
results = [0, 0] # First value is zeros, second is ones
for key, values in groupby(input_list):
results[key] += (2**len(tuple(values))) - 1
assert results == [6,9]
More Related Contents:
- Why doesn’t the operator module have a function for logical or?
- How to create the following lists with list comprehensions? [closed]
- why doesn’t += work in a while true loop python?
- UnicodeDecodeError: ‘charmap’ codec can’t decode byte X in position Y: character maps to
- Unpacking, extended unpacking and nested extended unpacking
- Local variable referenced before assignment?
- Python 3.7 anaconda environment – import _ssl DLL load fail error
- How to import members of all modules within a package?
- Difference between except: and except Exception as e:
- What is the naming convention in Python for variable and function?
- Generic[T] base class – how to get type of T from within instance?
- floating point in python gives a wrong answer [duplicate]
- Function chaining in Python
- Selenium gives “selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary” on Mac
- Checking for NaN presence in a container
- How to run a http server which serves a specific path?
- Why is a class __dict__ a mappingproxy?
- What is the difference between int() and floor() in Python 3?
- Python map object is not subscriptable [duplicate]
- Changing User Agent in Python 3 for urrlib.request.urlopen
- How to fix “no module named ‘app_one'”
- Python 3 bytes formatting
- Calling Python script from C++ and using its output
- json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) [duplicate]
- In Python, when are two objects the same?
- Why isn’t isnumeric working?
- Why are str.count(”) and len(str) giving different outputs when used on an empty string?
- Convert binary string to bytearray in Python 3
- ‘is’ operator behaves unexpectedly with floats
- Right-to-Left and Left-to-Right printed nicely