i have a large list like this ['z','z','z','z','e','e','e','z','z'] i want to print '4z3e2z' help please #31
|
i have a large list like this ['z','z','z','z','e','e','e','z','z'] |
Answered by
Tanu-N-Prabhu
Jul 16, 2025
Replies: 3 comments
|
def count_consecutive_chars(lst): lst = ['z','z','z','z','e','e','e','z','z'] |
0 replies
|
To solve this, you need to group consecutive identical elements in the list and count how many times each element appears consecutively. Here's how you can do it: from itertools import groupby
# Input list
data = ['z', 'z', 'z', 'z', 'e', 'e', 'e', 'z', 'z']
# Group consecutive identical elements and count occurrences
result = ''.join(f"{len(list(group))}{key}" for key, group in groupby(data))
# Print the result
print(result)
Output: 4z3e2z
|
0 replies
|
Try this ;) Explanation:
|
0 replies
Answer selected by
Tanu-N-Prabhu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this ;)
Explanation: