Как получить доступ к элементу словаря в цикле? - PullRequest
0 голосов
/ 16 апреля 2019

Я пытаюсь создать модель, которая будет предсказывать значения таймера трафика, определенные в словаре.Та же проблема уже сделана и смоделирована в MATLAB, но я получаю ниже ошибку в python при доступе к элементам из словаря.

import random
q_len = {'less': random.randrange(0,8), 'medium': random.randrange(6,14), 
'long':random.randrange(12,30)}
peak_hours = {'very_light':random.randrange(0, 7.5), 'heavy_ 
 morn':random.randrange(7,11.5), 'medium':random.randrange(11,16.5), 
'heavy_eve':random.randrange(16,20.5), 'light':random.randrange(20,24.5)}
time_ext = {'more_decrease':random.randrange(-32 -8), 
'decrease':random.randrange(-16 -1), 'do_not_change':random.randrange(-8 
-0.5), 'increase':random.randrange(0, 16), 
'more_increase':random.randrange(8, 32)}
def traffic(queue_len, peak_hours):
#entry1 = float(input('Enter the value of length of queue: '))
#entry2 = float(input('Enter the value of peak_hr: '))
#if entry1 in q_len && entry2 in peak_hours:
        for i in iter.items in q_len:
            for j in iter.items in peak_hours:
                if queue_len is less & peak_hours is very_light:
                    time_ext=more_decrease
                elif queue_len is less & peak_hours is heavy_morn:
                    time_ext=decrease
                elif queue_len is less & peak_hours is medium:
                    time_ext=decrease
                elif queue_len is less & peak_hours is heavy_eve :
                    time_ext=decrease
                elif queue_len is less & peak_hours is light:
                    time_ext=more_decrease
                elif queue_len is medium & peak_hours is very_light:
                    time_ext=increase
                elif queue_len is medium & peak_hours is heavy_morn:
                    time_ext=increase
                elif queue_len is medium & peak_hours is medium:
                    time_ext=do_not_change
                elif queue_len is medium & peak_hours is heavy_eve:
                    time_ext=increase
                elif queue_len is medium & peak_hours is light:
                    time_ext=do_not_change
                elif queue_len is long & peak_hours is very_light:
                    time_ext=do_not_change
                elif queue_len is long & peak_hours is heavy_morn:
                    time_ext=more_increase
                elif queue_len is long & peak_hours is medium:
                    time_ext=increase
                elif queue_len is long & peak_hours is heavy_eve:
                    time_ext=more_increase
                else:
                    time_ext=increase
print(time_ext)

Он должен печатать значение расширения времени после получения ввода из функциино это не то же самое.Пожалуйста, помогите, если кто-то знает, какую ошибку я совершаю.

Ответы [ 2 ]

1 голос
/ 16 апреля 2019

Для доступа к диктовке, два метода:

  1. q_len['less']
  2. q_len.get('less',''). Второй аргумент по умолчанию, если ключ 'less' не существует
0 голосов
/ 17 апреля 2019

Ваша ошибка в том, что randrange принимает только целые (целые числа) значения.Все дело в том, что он принимает случайные значения от range, который опять-таки поддерживает только целые числа.Так, например, random.randrange(0, 7.5) не будет работать, потому что 7.5 не является целым числом (в нем есть десятичная дробь).Если вам нужно случайное число от 0 до 7.5 с шагом 0.1, вы можете просто набрать random.randrange(0, 75)/10.

...