a = [112211121112121122111]
is a list with only one element.The answer to your question is effortless because there is only one element in your list.
I guess you mean a sequence like this:
a = [1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 2, 1, 2, 1, 1, 2, 2, 1, 1, 1]The items in the list are Integers, but they could also be str or something else.
Make a function, which saves the last_value, start_index and current index.
If there is a difference between last and current value, you've to add the last value with start_index, end_index to a list. Then you assign the current_value to last_value and the start_index. After your loop is done, you've to add the last remaining group.
Then you have a list with items where one value is the value itself, start_index and end_index.
You can get the shortest and longest group with a key function.
So if you save the groups like this pattern:
(value, start_index, end_index)You can get the longest and shortest distance by calculating it.
def by_distance(element): value, start, end = element return end - start result = [(42, 4, 22), (33, 22, 25)] print("Shortest") print(min(result, key=by_distance)) print("Longest") print(max(result, key=by_distance))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!