我正在尝试建立一个模型来预测词典中定义的流量计时器的值。在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)
从功能输入后,应打印时间扩展的值,但不打印相同的时间。如果有人知道我的错误,请提供帮助。
答案 0 :(得分:1)
要访问字典,有两种方法:
q_len['less']
q_len.get('less','')
。第二个参数是万一键'less'不存在时的默认值答案 1 :(得分:0)
您的错误是因为randrange
仅接受整数(整数)值。关键是它从range
中获取随机值,而random.randrange(0, 7.5)
再次仅支持整数。因此,例如7.5
无效,因为0
不是整数(其中有一个小数)。如果您希望以7.5
的步长从0.1
到random.randrange(0, 75)/10
随机数,则可以执行class UserAnimation : MoveAnimation
{
private double _defaultTranslationY;
public UserAnimation()
{
DurationIn = DurationOut = 300;
EasingIn = Easing.SinOut;
EasingOut = Easing.SinIn;
PositionIn = MoveAnimationOptions.Right;
PositionOut = MoveAnimationOptions.Right;
}
//1
public override void Preparing(View content, PopupPage page)
{
base.Preparing(content, page);
page.IsVisible = false;
if (content == null) return;
_defaultTranslationY = content.TranslationY;
}
//3
public override void Disposing(View content, PopupPage page)
{
base.Disposing(content, page);
page.IsVisible = true;
if (content == null) return;
content.TranslationY = _defaultTranslationY;
}
//2
public async override Task Appearing(View content, PopupPage page)
{
var taskList = new List<Task>();
taskList.Add(base.Appearing(content, page));
if (content != null)
{
var topOffset = GetTopOffset(content, page);
var leftOffset = GetLeftOffset(content, page);
taskList.Add(content.TranslateTo(content.Width, _defaultTranslationY, DurationIn, EasingIn));
};
page.IsVisible = true;
await Task.WhenAll(taskList);
}
//4
public async override Task Disappearing(View content, PopupPage page)
{
var taskList = new List<Task>();
taskList.Add(base.Disappearing(content, page));
if (content != null)
{
_defaultTranslationY = content.TranslationX - content.Width;
var topOffset = GetTopOffset(content, page);
var leftOffset = GetLeftOffset(content, page);
taskList.Add(content.TranslateTo(leftOffset, _defaultTranslationY, DurationOut, EasingOut));
};
await Task.WhenAll(taskList);
}
}
。