Thread: Lists problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Lists problem


    Given a list, my intention is to return a list, in which each element is another list, with the first element in that list being a element from the input list, and the second element being the count of the number of times that element appears in that input list.

    For example,
    INPUT = [1, 2, 2, 3, 3, 3, 2, 2, 1]
    OUTPUT =[[1, 1], [2, 2], [3, 3], [2, 2], [1, 1]]

    INPUT = [1, 4, 4, 2, 3, 1]
    OUTPUT = [[1, 1], [4, 2], [2, 1], [3, 1], [1, 1]]

    Here's my code fore the same:


    Code:
    def longest_repetition(p):
                  s = []         
                  for e in p:         
                      count = 1         
                      ind = p.index(e)         
                          if e == p[ind+1]:             
                              count += 1             
                              s.append([e, count])        
                          else:             
                              s.append([e, count])     
                   return s              
    print longest_repetition([1, 2, 2, 3, 3, 3, 2, 2, 1])

    However, this is the output that I am getting:

    [[1, 1], [2, 2], [2, 2], [3, 2], [3, 2], [3, 2], [2, 2], [2, 2], [1, 1]]




    Can anyone help me with this problem? It's been really bugging me for a couple of days now!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    Hello!

    I think the list function you are looking for is list.count(element) which will return the number of times element appears in the list.

    Code:
    def longest_repetition(List):
        listsWithCount = []         
        for element in List:         
            count = List.count(element);                  
            listsWithCount.append([element,count]);    
        return remove_duplicate_elements(listsWithCount);
    The problem with my code is that it doesn't eliminate duplicates. That's a new problem for ya, but a valid that's worth exploring

    It was a fun exercise, so I went ahead and solved the duplicate problem.

    Code:
    def isKeyInList(key,List):
        isKeyInList = False;
        for element in List:
            if element == key:
                isKeyInList = True;
        return isKeyInList;
    
    def remove_duplicate_elements(List):
        listWithNoDuplicates = [];
        for element in List:
            if not isKeyInList(element,listWithNoDuplicates):
                listWithNoDuplicates.append(element);
        return listWithNoDuplicates;
    Code:
    OUTPUT
    Input: [1, 2, 2, 3, 3, 3, 2, 2, 1]
    Results With Duplicates: [[1, 2], [2, 4], [2, 4], [3, 3], [3, 3], [3, 3], [2, 4], [2, 4], [1, 2]]
    Duplicates Eliminated: [[1, 2], [2, 4], [3, 3]]
    Last edited by Cameron0960; July 5th, 2013 at 10:40 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by Cameron0960
    Hello!

    I think the list function you are looking for is list.count(element) which will return the number of times element appears in the list.

    Code:
    def longest_repetition(List):
        listsWithCount = []         
        for element in List:         
            count = List.count(element);                  
            listsWithCount.append([element,count]);    
        return remove_duplicate_elements(listsWithCount);
    The problem with my code is that it doesn't eliminate duplicates. That's a new problem for ya, but a valid that's worth exploring

    It was a fun exercise, so I went ahead and solved the duplicate problem.

    Code:
    def isKeyInList(key,List):
        isKeyInList = False;
        for element in List:
            if element == key:
                isKeyInList = True;
        return isKeyInList;
    
    def remove_duplicate_elements(List):
        listWithNoDuplicates = [];
        for element in List:
            if not isKeyInList(element,listWithNoDuplicates):
                listWithNoDuplicates.append(element);
        return listWithNoDuplicates;
    Code:
    OUTPUT
    Input: [1, 2, 2, 3, 3, 3, 2, 2, 1]
    Results With Duplicates: [[1, 2], [2, 4], [2, 4], [3, 3], [3, 3], [3, 3], [2, 4], [2, 4], [1, 2]]
    Duplicates Eliminated: [[1, 2], [2, 4], [3, 3]]
    Hey! Thanks a lot for the help. However, what I need from the input is a list which contains the count of all the immediately occurring elements. For example,

    INPUT: [1, 2, 2, 3, 3, 3, 2, 2, 1]

    the output should contain the count of the immediate-occurring elements. Lets traverse through the list: first element is 1. Now, since the very next element is not one, then my first element in the output list would be [1, 1]. Similarly, for 2, since there are 2 back-to-back 2's, my second element in the output list would be [2, 2].

    So, the output I am looking for is:

    [[1, 1], [2, 2], [3, 3], [2, 2], [1, 1]]


    I am sorry. I should have mentioned it in my original post itself. Again, sorry for the inconvenience cost!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    Your description did not match your examples. What are we to do?

    First, let's write a test.
    Code:
        '''
            doctest.  Use:
            SHELL_PROMPT$ python -m doctest this_file.py
    
            >>> repetitions([1, 2, 2, 3, 3, 3, 2, 2, 1])
            [[1, 1], [2, 2], [3, 3], [2, 2], [1, 1]]
            >>> repetitions([1, 4, 4, 2, 3, 1])
            [[1, 1], [4, 2], [2, 1], [3, 1], [1, 1]]
        '''
    We declare success when we write a program that can non-trivially pass these tests. Next, it's apparent to me that that we need one pass through the list. Lo and behold, I got it right on my first attempt.
    Code:
    def repetitions(L):
        '''
            doctest.  Use:
            SHELL_PROMPT$ python -m doctest this_file.py
    
            >>> repetitions([1, 2, 2, 3, 3, 3, 2, 2, 1])
            [[1, 1], [2, 2], [3, 3], [2, 2], [1, 1]]
            >>> repetitions([1, 4, 4, 2, 3, 1])
            [[1, 1], [4, 2], [2, 1], [3, 1], [1, 1]]
        '''
        result = []
        i = 0
        while i < len(L):
            value = L[i]
            j = i + 1
            while (j < len(L)) and (value == L[j]):
                j += 1
            result.append([value, j - i])
            i = j
        return result
    If you need the longest you could sort with, roughly, (to be sure I'd look up the documentation)
    key = lambda a:a[1]
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo