0

The idea of the program is to count life time of some structures (count same structures which are one after another) but i have problem with implementing counter or enumerate function, I don't want to count number of all. I've tried do this many ways. Now I am stuck.

struct=[1, 2, 2, 2, 3, 3, 1, 2, 3, 1, 1, 1, 1, 2, 1, 1,] #input list of structures
list1=[] #list of lifetimes for struct 1

for i in range (0, len(struct)-1):
    a=struct[i-1]
    b=struct[i]
    c=struct[i+1]
    if a!=1 and b==1 and c!=1: # if it is one separated record
        list1.append(1)
    if a!=1 and b==1 and c==1: # for first record in sequence 
        list1.append('f')
    if a==1 and b==1 and c==1: # for middle record in sequence
        list1.append('m')
    if a==1 and b==1 and c!=1: # for last record in sequence 
        list1.append('l')

print (list1)

it gives me: [1, 1, 'f', 'm', 'm', 'l', 'f', 'l']
could you give me any advise how to implement counter ? for example ['f', 'm', 'm', 'l] (firs/middle/middle/last) is given from [1, 1, 1, 1] from the list of structure so it's 4 records to get [1, 1, 4, 2]

Sorry, for my non programming language, I am beginner in this field. I've search any similar questions but couldn't find.

13
  • Should all lines after for i in range (0, len(struct)-1): and before print (list1) be indented? Commented Jan 20, 2015 at 23:30
  • 1
    Why are you appending letters to your list if you want only numbers in the output? Commented Jan 20, 2015 at 23:33
  • Why are you appending, 'f', 'm', and 'l'? How are you expecting appending just 1, 'f', 'm', and 'l' to a list to give you a list that has a 2 and a 4 in it? And what is the meaning of [1, 1, 4, 2] with respect to this list anyway? Commented Jan 20, 2015 at 23:33
  • bad copy paste, they are all in for loop, if i clearly understand you Commented Jan 20, 2015 at 23:34
  • 1
    What are we counting? None of this makes any sense. Commented Jan 20, 2015 at 23:35

2 Answers 2

2

Is it something like this, you're looking for?

inlist = [1, 2, 2, 2, 3, 3, 1, 2, 3, 1, 1, 1, 1, 2, 1, 1,]
#         1                 1        1--2--3--4     1--2
def lifetime(num):
    output = []
    count = 0
    for x in inlist:
        if x == num:
            count += 1
        else:
            if count != 0:
                output.append(count)
                count = 0
    if count != 0:
        output.append(count)

    print output

lifetime(1)
lifetime(2)
lifetime(3)

Output:

[1, 1, 4, 2]
[3, 1, 1]
[2, 1]
Sign up to request clarification or add additional context in comments.

2 Comments

as @augurar said I need list of group lengths for each group of consecutive 1s in the list
YES! Thank you! Sorry for bothering in such a trivial case!
0

You can use itertools.groupby() to group the list by value, then return the counts for each group with a given value:

from itertools import groupby

def ilen(iterable):
    """ Return the length of an iterable """
    return sum(1 for _ in iterable)

def lifetime(num, lst):
    """ Return a list of counts for each group of num in lst """
    return [ilen(g) for k, g in groupby(lst) if k==1]

Example:

>>> inlist = [1, 2, 2, 2, 3, 3, 1, 2, 3, 1, 1, 1, 1, 2, 1, 1,]
>>> lifetime(1, inlist)
[1, 1, 4, 2]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.