4

I'm trying to return all the dates that match with the traffic intensity in the function below but am getting all the elements of only the first tuple as a list instead of all tuples that match.

def traffic_intensity(count):
   """Returns string indicating intensity level given by number of 
    vehicles"""
    level_name = ""
    if(count < 5000):
        level_name = "Very Low"
    elif(count >= 5000 and count < 10000):
        level_name = "Low"
    elif(count >= 10000 and count < 18000):
       level_name = "Moderate"
    elif(count >= 18000):
        level_name = "High"
    return(level_name)


def dates_with_intensity(vehicle_records, intensity):
    """Returns number of days with the given traffic intensity level"""
    new_list = []
    for number in vehicle_records:
        date_count = number[0]
        number_count = number[1]
        traffic = traffic_intensity(number_count)
        if traffic == intensity:
            new_list += date_count
        return new_list

For the test data

vehicle_records = [('2010-01-01',1),
                ('2010-01-02',2),
                ('2010-01-03',3)]
days = dates_with_intensity(vehicle_records, 'Very Low')
print(days)

I am supposed to be getting

['2010-01-01', '2010-01-02', '2010-01-03']

but instead am getting

['2', '0', '1', '0', '-', '0', '1', '-', '0', '1']

Can someone please help me with this?

  • 1
    Can you post some sample data that you expect this to work with (i.e `vehicle_records`)? – Joseph M Apr 17 '18 at 03:27
  • 1
    @JosephM just did :) –  Apr 17 '18 at 03:30
  • Just as an FYI, `count >= 5000 and count < 10000` can very neatly be replaced by `5000 <= count < 10000` in Python. Any sequence of rich comparison operators `a op1 b op2 c ...` gets effectively computed as `a op1 b and b op2 c ...`. The only difference is that if `a`, `b` or `c` are complex expressions, they only get evaluated once. – Mad Physicist Apr 17 '18 at 04:20
  • Also, you don't need to parenthesize the `if` and `elif` expressions. – Mad Physicist Apr 17 '18 at 04:21
  • Well posed question though.+1 – Mad Physicist Apr 17 '18 at 04:22

2 Answers2

1

Get the return out of the for loop. Also date_count isn't an array so you should be using .append() not addition:

def dates_with_intensity(vehicle_records, intensity):
"""Returns number of days with the given traffic intensity level"""
    new_list = []
    for number in vehicle_records:
        date_count = number[0]
        number_count = number[1]
        traffic = traffic_intensity(number_count)
        if traffic == intensity:
            new_list.append(date_count)
    return new_list
Primusa
  • 13,136
  • 3
  • 33
  • 53
0

There are two issues, one is return is inside for loop which causes to look into only one item. And other is you may need to extend array new_list with date_count. You can try as following:

def dates_with_intensity(vehicle_records, intensity):
    """Returns number of days with the given traffic intensity level"""
    new_list = []
    for number in vehicle_records:
        date_count = number[0]
        number_count = number[1]

        traffic = traffic_intensity(number_count)
        if traffic == intensity:
            new_list += [date_count] # extend as array
    return new_list # move return

For testing similar scenario, you can consider following example:

l = []
l += "string"
print(l)

Result:

['s', 't', 'r', 'i', 'n', 'g']

Instead:

l = []
l += ["string"]
print(l)

Result:

['string']

For more, I would suggest looking into relevant discussion.

Update:

I think the code can be little shorter with list comprehension and removing condition in elif with following:

def traffic_intensity(count):
    """Returns string indicating intensity level given by number of vehicles"""
    if (count < 5000): return "Very Low"
    elif(count < 10000): return "Low"
    elif(count < 18000): return "Moderate"
    else: return "High"
    

def dates_with_intensity(vehicle_records, intensity):
    """Returns number of days with the given traffic intensity level"""
    return [date for date, num in vehicle_records if intensity == traffic_intensity(num)]
Community
  • 1
  • 1
niraj
  • 17,498
  • 4
  • 33
  • 48
  • You can also unpack in a `for`, for more descriptive variable names e.g. `[date for date, num in ... traffic_intensity(num)]` – AChampion Apr 17 '18 at 04:16
  • @AChampion (*edited*) thanks! I think it makes more easier to understand. – niraj Apr 17 '18 at 04:19