Python Forum
update dict as per range of values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
update dict as per range of values
#7
Here a different approach. Before you must do a transformation, that all ports are in a list.

unsorted_ports = [item['port'] for item in data]
def find_port_ranges(ports):
    # the set returns only unique elements
    # then the set is sorted in reversed order
    # the resulting object is a sorted list
    ports = sorted(set(ports), reverse=True)
    # pop the last element from list, which is the smallest
    start = last = ports.pop()
    # start indicates the start range
    # last is always the value from previous iteration
    # repeat until the list ports is empty
    while ports:
        # pop the last element from list
        value = ports.pop()
        if value - 1 != last:
            yield (start, last)
            start = last = value
        else:
            # last value is current value - 1
            last = value
    # yield the last elements
    yield (start, value)
Test:
Output:
>>> example_ports = [1,2,4,3,20,10,11,13,12,21,23,28,22,25,26,29,100,101,103] >>> list(find_port_ranges(example_ports)) [(1, 4), (10, 13), (20, 23), (25, 26), (28, 29), (100, 101), (103, 103)]
A second function can do the formatting:

def format_port_ranges(ports):
    ranges = []
    for start, end in find_port_ranges(ports):
        if start != end:
            ranges.append(f'{start}-{end}')
        else:
            ranges.append(str(start))
    return 'Ports: ' + ', '.join(ranges) # split ranges by comma
Output:
>>> format_port_ranges([1,2,3,4,10,20,11,12,13,21,22,23,22,25,26,29]) 'Ports: 1-4, 10-13, 20-23, 25-26, 29'
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
update dict as per range of values - by anna - Sep-04-2019, 01:13 PM
RE: update dict as per range of values - by anna - Sep-05-2019, 06:24 AM
RE: update dict as per range of values - by anna - Sep-09-2019, 06:48 AM
RE: update dict as per range of values - by luoheng - Sep-09-2019, 07:37 AM
RE: update dict as per range of values - by anna - Sep-09-2019, 11:01 AM
RE: update dict as per range of values - by DeaD_EyE - Sep-09-2019, 11:07 AM
RE: update dict as per range of values - by anna - Sep-13-2019, 04:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  dict class override: how access parent values? Andrey 1 1,745 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  matplotlib x axis range goes over the set range Pedroski55 5 3,397 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Removing nan values from a dict tomtom 8 7,519 Oct-05-2021, 06:44 PM
Last Post: tomtom
  How can I count values between range dates ? Eidrizi 2 2,589 Mar-17-2021, 01:26 PM
Last Post: Eidrizi
  How to update values in a pyarrow table? xraphael75 1 3,779 Jan-25-2021, 02:14 PM
Last Post: xraphael75
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,813 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  Looking for help in Parse multiple XMLs and update key node values and generate Out.. rajesh3383 0 1,933 Sep-15-2020, 01:42 PM
Last Post: rajesh3383
  Trouble with converting list , dict to int values! faryad13 7 3,911 Sep-04-2020, 06:25 AM
Last Post: faryad13
  Sort a dict in dict cherry_cherry 4 95,983 Apr-08-2020, 12:25 PM
Last Post: perfringo
  How to access specific values from a dict? t4keheart 6 3,256 Feb-05-2020, 11:34 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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