-2

I have a live process the could be true or false in input. Based on the condition status, if it's true start to write the timecode in a file. I need to write the timecode just the first time the condition is true and add the number of times the condition was true. So, if the statement is true 5 times, I need to write the timecode the first time the condition it's true, ignoring the next true condition, but counting them and write the times the condition was true in the file.

if process:
         writeTC(fName,
                  TC_in, # write only the first time the condition is true
                  )
         writeDuration(fName,
                       Duration # write duration only at the last true cond.
                       ) 
))

Output: 00:00:03:05 7 sec.

Old vague question.

Before answering, please read my question. I now how to use counter, what I'm asking is about the if statement. Thanks. I have a condition inside a loop. I want to print the result once, the first time the condition is true and add the number of times the condition was true.

arr = ['a', 1, 1, 1, 1, 1, 2, 2, 2, 2, 'a', 'a',
       3, 3, 'a', 4]

for i in arr:
    if type(i) == int:
        print('Printed once {i}'.format(i=i))

I would like end up with this result:

There is 5 times the number 1
There is 4 times the number 2
There is 2 times the number 3
There is 1 times the number 4
E_net4
  • 27,810
  • 13
  • 101
  • 139
imnewhere
  • 96
  • 11
  • what are you trying to say , pls elaborate – MrHola21 Apr 25 '21 at 08:20
  • Does this answer your question? [Python how to check the type of a variable](https://stackoverflow.com/questions/49010949/python-how-to-check-the-type-of-a-variable) – Marcin Orlowski Apr 25 '21 at 08:21
  • @MarcinOrlowski I am not sure that is a right dupe, OP seems to print something based on a counter of sorts – python_user Apr 25 '21 at 08:21
  • You want the collections.Counter https://www.guru99.com/python-counter-collections-example.html – michjnich Apr 25 '21 at 08:22
  • Hi, exactly what I wrote, if the condition is true, I'd like to do something, if it is true again I don't want to do anything except count the condition was true again print the time was true. – imnewhere Apr 25 '21 at 08:22
  • [How can I count the occurrences of a list item?](https://stackoverflow.com/q/2600191/3890632) – khelwood Apr 25 '21 at 08:23

3 Answers3

0

This will work.I think Counter is the best function to count occurences in a list.

from collections import Counter
arr = ['a', 1, 1, 1, 1, 1, 2, 2, 2, 2, 'a', 'a',
       3, 3, 'a', 4]
filteredArr = [x for x in arr if type(x)==int]    
k = dict(Counter(filteredArr))
for i in k.keys():
    print('There is {} the number {}'.format(k[i],i))
Pramod Kumar
  • 1,099
  • 1
  • 4
  • 12
  • I don't want count the array, I wrote that I want to count the conditional statement. I knew the counter. I'm asking another think. – imnewhere Apr 25 '21 at 08:35
  • @imnewhere it does exactly what you're asking for. – bereal Apr 25 '21 at 09:10
  • Just 2 cents: by using a genexpr: `filtered = (x for x in arr...)`, you will avoid creating a redundant intermediate list. – bereal Apr 25 '21 at 09:11
0

you can use Counter --

from collections import Counter
arr = ['a', 1, 1, 1, 1, 1, 2, 2, 2, 2, 'a', 'a',
       3, 3, 'a', 4]

freq_dict = Counter([item for item in arr if type(x)==int])
for key,value in freq_dict.items():
    print(f'There is {value} the number {key}')

Output -

There is 5 the number 1
There is 4 the number 2
There is 2 the number 3
There is 1 the number 4
Nk03
  • 14,699
  • 2
  • 8
  • 22
0

I'm still not sure when you want to do the print statement, but may be a dictionary could be closer to what you want:

import time

arr = ['a', 1, 1, 1, 1, 1, 2, 2, 2, 2, 'a', 'a', 3, 3, 'a', 4]

numbers = {}

# extract all integers from the starting list
for element in arr:
    if type(element) == int:
        if element not in numbers.keys():
            numbers.update({element: [time.time_ns(), 1]})
        else:
            numbers[element][1] += 1

for key in numbers.keys():
    print(f"the number {key} occurs {numbers[key][1]} times. First occurrence at {numbers[key][0]} ")

First time an interger is found, it is written to the dictionary as key. The corresponding value takes the actual time and a counter.

Next time this integer is found, only the counter is increased.

So If you need the print statement while looping, just add it into the innerif else clause.

AnPla
  • 32
  • 1
  • 5