'Find how many adjacent zeros in given list like this [100,1010,1001,1111,10100]
I have tried this
list1=[100,1010,1001,1111,10100]
print(list1.count(00))
I got this
0
Expected Output:
3
Solution 1:[1]
Did you try:
list1 = [100,1010,1001,1111,10100]
print("".join(str(x) for x in list1).count("00"))
Prints: 3
Solution 2:[2]
list.count(00) will only give you the number of occurrences of the value '00' in the list.
To look for the characters 00 within each list item you will need to iterate through the list and look at each item.
Easiest check would be to convert the value to a string and then check for the double 00 characters within that
Solution 3:[3]
IIUC, you could filter and count:
count = sum(1 for e in list1 if '00' in str(e))
output: 3
Or, if a number like 10000 should count for 2 occurrences of 00:
count = sum(str(e).count('00') for e in list1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cresht |
| Solution 2 | Dragonel |
| Solution 3 |
