0

I have input like below,

Input 
_____
False
True
True
True
True
False
False
False
False
False
False
True
True
True
True
False
False
False
False
False
False
False

I am looking for this output,

False   1
True    2
True    2
True    2
True    2
False   3
False   3
False   3
False   3
False   3
False   3
True    4
True    4
True    4
True    4
False   5
False   5
False   5
False   5
False   5
False   5
False   5

You can assume any type for the input data (i.e., list or pandas series) etc.,

I tried something like this but it's not the output I need. Any help is appreciated.

current_value = None
current_count = 0

input_array = [False, True, True, True, True, False, False, False, False,
               False, False, True, True, True, True, False, False, False,
               False, False, False, False]
for line in input_array:
  # Convert the line to a boolean value
  value = line == True

  if value != current_value:
    if current_value is not None:
      print('{}\t{}'.format(current_value, current_count))
    current_value = value
    current_count = 1
  else:
    current_count += 1
user_12
  • 1,778
  • 7
  • 31
  • 72
  • You tag the question as pandas, but the current code uses no pandas dataframe nor series. Do you want a pandas way, or a plain Python one? – Serge Ballesta Dec 08 '22 at 14:48
  • So, first, it's good to specify what the output you're actually getting is, if it's not what you need. That said, your current code looks like it's counting the length of consecutive runs, rather than the number. For that you should increment each time it changes, rather than when it stays the same, and never reset it to 1 – Edward Peters Dec 08 '22 at 14:48
  • @SergeBallesta Any solution is fine for me. Pandas or python one. – user_12 Dec 08 '22 at 14:50
  • 1
    pandas: `df = pd.DataFrame({'input': input_array}) ; df['Counter'] = df['input'].ne(df['input'].shift()).cumsum()` ; pure python: `counter = [x for i,(_,g) in enumerate(itertools.groupby(input_array), start=1) for x in [i]*len(list(g))]` – mozway Dec 08 '22 at 14:51
  • `value = line.strip() == 'True'` - inputArray is already boolean. You likely copied code that reads a file. But make the example you post here runnable. Just take this string to bool conversion out because its not part of the problem. – tdelaney Dec 08 '22 at 14:52
  • 1
    I also added a python solution above if you want. – mozway Dec 08 '22 at 14:54
  • @tdelaney Sorry it was a mistake, I have the values as string. I have updated the question. – user_12 Dec 08 '22 at 14:59
  • 1
    Another python solution: `chain.from_iterable(zip(v, repeat(i)) for i, (_,v) in enumerate(groupby(input_array), start=1))` – Jab Dec 08 '22 at 15:09

1 Answers1

1

When trying to run your snippet, I actually got an error - 'bool' object has no attribute 'strip'. Having the .strip() is great if everything is a string, but will not work with other data types.

In addition, you're outputting the values before doing anything with them.

Finally, your logic at the bottom is reversed - you only want to increment if it's a different value, right?

Try something like this:

current_value = None
current_count = 0

input_array = [False, True, True, True, True, False, False, False, False,
               False, False, True, True, True, True, False, False, False,
               False, False, False, False]
for line in input_array:
    if line != current_value:
        current_value = line
        current_count += 1

    print(current_value, current_count)

... which gives the following output

code output

Luke
  • 1,060
  • 8
  • 19
  • I have the inputs as strings in my data. I have updated the question now. It should work now. – user_12 Dec 08 '22 at 15:00
  • @user_12 to be honest, unless your strings are vastly different (as in "True ", "True" and "True "), I'm not convinced that you *need* the `strip`. `"True" = "True"` as much as `True = True`, and omitting the `strip` means it will also function if you have non-string data types – Luke Dec 08 '22 at 15:22