python根据字典的key获取对应的value值进行判断

# -*- coding:utf-8 -*-

def pred(dayToday, fields):
    '''
    :param dayToday: 当前日期
    :param fields: n行m列的特征数据
    :return:
    '''
    day_threshold = {'1': 0.1, '2': 0.2, '3': 0.3, '4': 0.4, '5': 0.5, '6': 0.6, '7': 0.7}    # 日期-阈值字典
    if str(dayToday) in day_threshold.keys():     # 判断日期是否在字典的key中
        threshold = day_threshold[str(dayToday)]  # 获取日期对应key的value值
        print(threshold)
        # if float(fields['a']) / float(fields['b']) < day_threshold[str(dayToday)]:
        if float(10.0) / float(100.0) < threshold:
            return True
        else:
            return False
    else:
        return False

dayToday = 3
fields = None
overUsed = pred(dayToday, fields)   # 调用函数
print(overUsed)

输出结果如下:

0.3
True

你可能感兴趣的:(字典处理,python)