Home > database >  Accessing elements in a list of ranges in Python
Accessing elements in a list of ranges in Python

Time:10-05

I've created a function that takes a number N from the user, and then asks for a pair of numbers from the user N times. Those pairs are appended to a list as a range. The function then returns the list of ranges.

In another function, I want to find the number of unique elements within those ranges. So that list is passed to this function. However, I'm unsure of how to grab the numbers from each range in the list. I wanted to create a set and add each number in each range to that set. I created a for loop to run through each element in the list. However, when I created an empty variable and set it to the list at x element, I'm given an error that reads range object cannot be interpreted as an integer. Next, I tried to initialize an empty list and set that to each element in the list, but I was given an error that reads list indices must be integers or slices, not range.

Here's an example of part of my code:

def get_input():
    
    num_events = int(input())
    events = []
    
    for i in range(0, num_events):
        si, ti = (input("Enter the start and end days of event #"   str(i   1)   ": ").split())
        events.append(range(int(si), int(ti)))
    
    return events
def calculate(events):
    
    new_set = set()
    
    for l in events:
        
        check = events[l]
        new_set.add(check)
        
    unique_days = len(new_set)
        
    return unique_days

CodePudding user response:

This is because you're trying to access to a slice of your element. Let me explain :

When you write for l in events l is a range object. You can't use [] notation on range objects.

Last, you want the sum of unique days.

Try this :

def get_input():

    num_events = int(input())
    events = []

    for i in range(0, num_events):
        si, ti = (input("Enter the start and end days of event #"  
                  str(i   1)   ": ").split())
        events.append(range(int(si), int(ti)))

    return events


def calculate(events):

    new_set = set()
    unique_days = 0
    for check in events:

        new_set.add(check)

        unique_days  = len(check)

    return unique_days

With this code, the output would be for example :

events = get_input()
# num_events = 2
# date for day 1 : 1 and 3
# date for event 2 : 4 and 5
ud = calculate(events)
print(ud) # print (3-1)   (5-4) = 2   1 = 3

CodePudding user response:

As I said in a comment your get_input() is creating a list of range objects, not sublists of values in the range. Fixing that simply involves creating the sublists when needed (actually for sets you need values which are hashable sequences like a tuple not a list).

def get_input():

    intervals = (1, 3), (2, 6), (5, 7) # Hardcode some input data for testing.
    events = [range(a, b) for a, b in intervals]
    return events

#    num_events = int(input())
#    events = []
#
#    for i in range(0, num_events):
#        si, ti = (input("Enter the start and end days of event #"   str(i   1)   ": ").split())
#        events.append(range(int(si), int(ti)))
#
#    return events

def calculate(events):

    new_set = set()
    for event in events:
        check = tuple(event)  # Convert range into a tuple (a hashable sequence)
        new_set.add(check)
    print(f'{new_set=}')  # -> new_set={(1, 2), (2, 3, 4, 5), (5, 6)}

    unique_days = len(new_set)
    return unique_days


events = get_input()
print(f'{events=}')  # -> events=[range(1, 3), range(2, 6), range(5, 7)]

unique_days = calculate(events)
print(f'{unique_days=}')  # -> unique_days=3

  • Related