Return date with traffic intensity from tuples in the list

I try to return all dates that correspond to the traffic intensity in the function below, but I get all the elements of only the first tuple as a list, and not all the 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 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 have to get

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

but instead i get

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

Can someone please help me with this?

+4
source share
2 answers

Get a return from the for loop. Also date_count is not an array, so you should use .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
+1
source

: return for, . new_list date_count. :

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

:

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

:

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

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

:

['string']

, .

Update:

, list comprehension elif :

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)]
0

Source: https://habr.com/ru/post/1696237/


All Articles