Python Forum
detect equal sequences in list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
detect equal sequences in list
#3
Two years posting to this forum disqualifies you as a python beginner. That's about how long I've been using Python.

You don't seem to fully understand classes. It looks like you think they work just like functions.

Normally you don't work directly with classes, you work with objects. To get a HangerFinder object you need to create an instance of class HangerFinder. After creating the object you use the object to call the detect_picture_nr() method. The code should look something like this:
class HangerFinder:
    def __init__(self, hash_list_film):
        """This is called when you create an instance of the class.  Initialize things here."""
        # Would this be a good place to make the hash list?  Pass in a film list and compute the hash list?
        self.hash_list = hash_list_film
        self.start = 0

    def detect_picture_nr(self, start=None, end=None):
        """ This is a method that returns the start and end index of a matching sequence.
        It uses instance variables that were created by the __init__().
        """
        if end is not None:
            end = min(end, len(self.hash_list))
        else:
            end = len(self.hash_list)
        if start is not None:
            self.start = max(0, min(end, start))
        start = self.start

        if start >= end:
            return None

        for i in range(start+1, end):
            if self.hash_list[i] != self.hash_list[i-1]:
                self.start = i
                return start, i-1
        self.start = end
        return start, end-1


# Create an instance
finder = HangerFinder(["12", "11", "11", "11", "17", "22", "22", "22", "22", "23"])

# Use the instance
while (range_ := finder.detect_picture_nr()):
    print(range_)
Output:
(0, 0) (1, 3) (4, 4) (5, 8) (9, 9)
But you really don't need a class for something like this. I would write it as a generator.
def hangers(frame_list, hash_func):
    """Return list of sequential frames that have the same hash value"""
    if frame_list:
        start = 0
        prev = hash_func(frame_list[start])
        for index, film in enumerate(frame_list[1:], start=1):
            next = hash_func(film)
            if next != prev:
                yield frame_list[start:index]
                start = index
                prev = next
        yield frame_list[start:]

# Using your number list to represent a frame list.  Using hash() in place of your hash function
for hanger in hangers(["12", "11", "11", "11", "17", "22", "22", "22", "22", "23"], hash):
    print(hanger)
Output:
['12'] ['11', '11', '11'] ['17'] ['22', '22', '22', '22'] ['23']
The same function without requiring frames to be indexable. This lets you use a lazy iterator instead of having to make a big list containing all the frames.
def hangers(frames, hash_func):
    """Return lists of sequential frames with the same hash value
    frames: An iterable of things to hash.
    hash_func: Function that returns a hash value.
    """
    frame_iter = iter(frames)
    seq = [next(frame_iter)]
    prev_hash = hash_func(seq[0])
    for frame in frame_iter:
        next_hash = hash_func(frame)
        if prev_hash != next_hash:
            yield(seq)
            seq = []
            prev_hash = next_hash
        seq.append(frame)
    yield seq

# Using your number list to represent a frame list.  Using hash() in place of your hash function
for hanger in hangers(iter(["12", "11", "11", "11", "17", "22", "22", "22", "22", "23"]), hash):
    print(hanger)
Reply


Messages In This Thread
detect equal sequences in list - by flash77 - Oct-14-2022, 09:10 AM
RE: detect equal sequences in list - by Yoriz - Oct-14-2022, 10:28 AM
RE: detect equal sequences in list - by deanhystad - Oct-14-2022, 07:07 PM
RE: detect equal sequences in list - by flash77 - Oct-15-2022, 08:05 AM
RE: detect equal sequences in list - by deanhystad - Oct-15-2022, 12:05 PM
RE: detect equal sequences in list - by flash77 - Oct-15-2022, 09:10 PM
RE: detect equal sequences in list - by Pedroski55 - Oct-16-2022, 12:53 AM
RE: detect equal sequences in list - by DPaul - Oct-16-2022, 06:36 AM
RE: detect equal sequences in list - by flash77 - Oct-16-2022, 05:27 PM
RE: detect equal sequences in list - by flash77 - Oct-26-2022, 09:30 AM
RE: detect equal sequences in list - by flash77 - Oct-26-2022, 12:00 PM
RE: detect equal sequences in list - by deanhystad - Oct-26-2022, 05:43 PM
RE: detect equal sequences in list - by flash77 - Oct-27-2022, 06:53 AM
RE: detect equal sequences in list - by deanhystad - Oct-27-2022, 08:04 AM
RE: detect equal sequences in list - by flash77 - Oct-27-2022, 08:35 AM
RE: detect equal sequences in list - by flash77 - Oct-27-2022, 09:13 AM
RE: detect equal sequences in list - by deanhystad - Oct-27-2022, 03:56 PM
RE: detect equal sequences in list - by flash77 - Oct-28-2022, 06:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 947 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Regarding how to randomizing list with having equal probability radraw 14 2,426 Nov-06-2022, 11:09 PM
Last Post: Pedroski55
  is there equal syntax to "dir /s /b" kucingkembar 2 1,072 Aug-16-2022, 08:26 AM
Last Post: kucingkembar
  Can a variable equal 2 things? Extra 4 1,589 Jan-18-2022, 09:21 PM
Last Post: Extra
  needleman wunsch algorithm for two sequences of different length johnny_sav1992 0 1,755 Jul-27-2020, 05:45 PM
Last Post: johnny_sav1992
  help for escape sequences NewPi 1 2,098 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  Not equal a dictionary key value bazcurtis 2 1,997 Dec-11-2019, 11:15 PM
Last Post: bazcurtis
  copying parts of mutable sequences Skaperen 1 2,308 Dec-02-2019, 10:34 AM
Last Post: Gribouillis
  Convert weekly sequences to date and time. SinPy 0 1,514 Nov-23-2019, 05:20 PM
Last Post: SinPy
  Escape sequences display in python Uchikago 1 2,509 Jun-27-2019, 03:25 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020