Skip to main content

Groups list items using a compare function

Project description

grplist

Groups list items using a compare function.

    groups = grplist([1, 3, 6, 10, 12, 14, 21, 35], lambda a, b: 3 >= abs(a-b), True)

    # groups => [[1, 3, 6], [10, 12, 14], [21], [35]]

Table of contents

 


Install

$ pip3 install grplist

 


Examples

    import grplist as gl
    import numpy as np

    try:
        import sparen
        Log = sparen.Log
    except Exception as e:
        Log = print

    def showBreak():
        print('\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n')

    def test_1():

        showBreak()

        tests = [
            [1, 6, 3],
            [10, 20, 30, 40, 50],
            [1, 3, 6, 10, 12, 14, 21, 35],
            [1, 3, 6, 10, 12, 14, 21, 35, 7, 23],
            [1, 10, 20, 5, 15, 3, 7],
            [1, 10, 20, 30, 40, 35, 32, 5, 11, 2, 3, 16, 17, 12, 33, 34, 35, 33, 3, 42],
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28],
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
        ]

        for t in tests:

            def cmpVal(a, b):
                test_1.c += 1
                return 3 >= abs(a-b)

            test_1.c = 0
            m = gl.grplist(t, cmpVal, True)
            Log(test_1.c, " : ", t, " -> ", m)

            test_1.c = 0
            m = gl.grplist2(t, cmpVal, True)
            Log(test_1.c, " : ", t, " -> ", m)

    def test_2():

        showBreak()
        a = {'k0': 1, 'k1': 3, 'k2': 6, 'k3': 10, 'k4': 12, 'k5': 14, 'k6': 21, 'k7': 35, 'k8': 7, 'k9': 23}

        m = gl.groupDict(a, lambda a, b: 3 >= abs(a-b), False)
        Log(a, " -> ", m) # [['k0', 'k1', 'k2', 'k3', 'k4', 'k5', 'k8'], ['k6', 'k9'], ['k7']]

        m = gl.groupDict(a, lambda a, b: 3 >= abs(a-b), True)
        Log(a, " -> ", m) # [['k0', 'k1', 'k2'], ['k3', 'k4', 'k5', 'k8'], ['k6', 'k9'], ['k7']]


    # Group letters
    def test_3():

        showBreak()

        def anyLetters(a, b):
            for l in a:
                if 0 <= b.find(l):
                    return True
            return False

        t = ['on', 'tw', 'th', 'fo', 'fi', 'si', 'te', 'zk']
        Log(t, " -> ", gl.grplist2(t, anyLetters, True))


    # Group factors
    def test_4():

        showBreak()

        t = [3, 4, 5, 6, 7, 8, 9, 10]
        Log(t, " -> ", gl.grplist2(t, lambda a, b: not (a % b), True))


    # Group overlapping tracks
    def test_5():

        showBreak()

        def showTracks(t, bs, es):
            n = 0
            print(' : 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8')
            for i in t:
                s = (' ' * i[bs]) + ('-' * (i[es] - i[bs] + 1))
                s = '|'.join(s.replace('-', '|', 1).rsplit('-', 1))
                print("%s: %s" % (n, s))
                n += 1

        t = [
            {'beg': 2,  'end': 10},
            {'beg': 20, 'end': 25},
            {'beg': 4,  'end': 7},
            {'beg': 30, 'end': 35},
            {'beg': 8,  'end': 17},
            {'beg': 22, 'end': 28},
            {'beg': 33, 'end': 45},
            {'beg': 1,  'end': 4},
            # {'beg': 0,  'end': 74},
        ]

        print("\n--- INPUT ---")
        showTracks(t, 'beg', 'end')

        g = gl.grplist2(t, lambda a, b: a['beg'] <= b['end'] and a['end'] >= b['beg'], True)

        i = 0
        for t in g:
            i += 1
            print("\n--- GROUP %s ---" % i)
            showTracks(t, 'beg', 'end')


    def main():
        test_1()
        test_2()
        test_3()
        test_4()
        test_5()


    if __name__ == '__main__':
        main()

Output

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    3  :  [1, 6, 3]  ->  [[1, 6, 3]]
    3  :  [1, 6, 3]  ->  [[1, 3, 6]]
    10  :  [10, 20, 30, 40, 50]  ->  [[10], [20], [30], [40], [50]]
    10  :  [10, 20, 30, 40, 50]  ->  [[10], [20], [30], [40], [50]]
    28  :  [1, 3, 6, 10, 12, 14, 21, 35]  ->  [[1, 3, 6], [10, 12, 14], [21], [35]]
    28  :  [1, 3, 6, 10, 12, 14, 21, 35]  ->  [[1, 3, 6], [10, 12, 14], [21], [35]]
    45  :  [1, 3, 6, 10, 12, 14, 21, 35, 7, 23]  ->  [[1, 3, 6, 10, 12, 14, 7], [21, 23], [35]]
    43  :  [1, 3, 6, 10, 12, 14, 21, 35, 7, 23]  ->  [[1, 3, 6, 7, 10, 12, 14], [35], [21, 23]]
    21  :  [1, 10, 20, 5, 15, 3, 7]  ->  [[20], [1, 10, 5, 3, 7], [15]]
    21  :  [1, 10, 20, 5, 15, 3, 7]  ->  [[1, 3, 5, 7, 10], [15], [20]]
    190  :  [1, 10, 20, 30, 40, 35, 32, 5, 11, 2, 3, 16, 17, 12, 33, 34, 35, 33, 3, 42]  ->  [[1, 5, 2, 3, 3], [10, 11, 12], [20, 16, 17], [30, 35, 32, 33, 34, 35, 33], [40, 42]]
    172  :  [1, 10, 20, 30, 40, 35, 32, 5, 11, 2, 3, 16, 17, 12, 33, 34, 35, 33, 3, 42]  ->  [[1, 3, 3, 2, 5], [10, 12, 11], [20, 17, 16], [30, 33, 33, 35, 34, 32, 35], [40, 42]]
    153  :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]  ->  [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]]
    57  :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]  ->  [[1, 4, 7, 10, 13, 16, 18, 17, 15, 14, 12, 11, 9, 8, 6, 5, 3, 2]]
    153  :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28]  ->  [[1, 2, 3, 4, 5, 6, 7, 8, 9], [20, 21, 22, 23, 24, 25, 26, 27, 28]]
    111  :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28]  ->  [[1, 4, 7, 9, 8, 6, 5, 3, 2], [20, 23, 26, 28, 27, 25, 24, 22, 21]]
    210  :  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]  ->  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
    20  :  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]  ->  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    {'k0': 1, 'k1': 3, 'k2': 6, 'k3': 10, 'k4': 12, 'k5': 14, 'k6': 21, 'k7': 35, 'k8': 7, 'k9': 23}  ->  [['k0', 'k1', 'k2', 'k3', 'k4', 'k5', 'k8'], ['k6', 'k9'], ['k7']]
    {'k0': 1, 'k1': 3, 'k2': 6, 'k3': 10, 'k4': 12, 'k5': 14, 'k6': 21, 'k7': 35, 'k8': 7, 'k9': 23}  ->  [[1, 3, 6, 10, 12, 14, 7], [21, 23], [35]]

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    ['on', 'tw', 'th', 'fo', 'fi', 'si', 'te', 'zk']  ->  [['on', 'fo', 'fi', 'si'], ['tw', 'te', 'th'], ['zk']]

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    [3, 4, 5, 6, 7, 8, 9, 10]  ->  [[3, 9, 6], [4, 8], [5, 10], [7]]

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    --- INPUT ---
    : 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8
    0:   |-------|
    1:                     |----|
    2:     |--|
    3:                               |----|
    4:         |--------|
    5:                       |-----|
    6:                                  |-----------|
    7:  |--|

    --- GROUP 1 ---
    : 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8
    0:   |-------|
    1:  |--|
    2:         |--------|
    3:     |--|

    --- GROUP 2 ---
    : 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8
    0:                     |----|
    1:                       |-----|

    --- GROUP 3 ---
    : 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8
    0:                               |----|
    1:                                  |-----------|

 


References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

grplist-0.1.4-py2.py3-none-any.whl (6.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file grplist-0.1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: grplist-0.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.5

File hashes

Hashes for grplist-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 823d28c658376ad578ef69a8dc83b7af87e0f025df58665edd4a0c7749d0be13
MD5 d227bc7ed24559e368b228df0717c209
BLAKE2b-256 d9e996bdba75362bf0c685879387e8feb410a4e733becc533b0fe178e244e7e9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page