迭代列表字典中的两个元素中的任何一个 [英] Iterate any of the two elements in the dictionary of the list

查看:53
本文介绍了迭代列表字典中的两个元素中的任何一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下所示的字典

d4 = {
    "blue": 
        [
          {
              "type": "linear",
              "start_date": "2020-10-01T20:00:00.000Z",
              "end_date": "2020-10-20T20:00:00.000Z",
              "n_days":3,
              "coef":[0.1,0.1,0.1,0.1,0.1,0.1],
              "case":"best"
          }]}

d5 = {
    "white": 
        [
          {
              "type": "linear",
              "start_date": "2020-10-01T20:00:00.000Z",
              "end_date": "2020-10-20T20:00:00.000Z",
              "n_days":3,
              "coef":[0.1,0.1,0.1,0.1,0.1,0.1],
              "case":"best"
          }]}

我的函数的输入可以是d4或d5.

Input to my function can be d4 or d5.

在某些情况下,开始日期,结束日期和n_days不可用.由start_date = 0,end_date = 0,n_days = 0指定的非可用性.

There might be a case where, start_date, end_date and n_days are not available. non availability specified by start_date = 0, end_date = 0, n_days = 0.

要解决这个问题,我们必须在8个条件下应用.

To tackle that we have to apply below 8 conditions.

那么我们将有8个条件

1. if (start_date != 0) and (end_date != 0) and (n_days != 0):
          end_date = start_date + n_days

2. if (start_date != 0) and (end_date != 0) and (n_days == 0):
          pass
    
3. if (start_date != 0) and (end_date == 0) and (n_days != 0):
          end_date = start_date + n_days

4. if (start_date != 0) and (end_date == 0) and (n_days == 0):
          print("Please enter required inputs")

5. if (start_date == 0) and (end_date != 0) and (n_days != 0):
          start_date = end_date - n_days

6. if (start_date == 0) and (end_date != 0) and (n_days == 0):
          print("Please enter required inputs")

7. if (start_date == 0) and (end_date == 0) and (n_days != 0):
          print("Please enter required inputs")

8. if (start_date == 0) and (end_date == 0) and (n_days == 0):
          print("Please enter required inputs")

通过应用上述条件,我想找出结束日期和开始日期.

By applying above condition I would like find out the end_date and start_date.

我尝试了以下代码:

def validate(d):
    REQUIRED_KEYS = ["blue", "white"]
    for bluewhite_category in REQUIRED_KEYS[0] or REQUIRED_KEYS[1]:
        #print(bluewhite_category)
        if bluewhite_category in REQUEST_OBJ.keys():
            param_obj_list = REQUEST_OBJ[bluewhite_category]
            n_days, start, end = param_obj_list['n_days'], 
                                 param_obj_list['start_date'], 
                                 param_obj_list['end_date']

            if start == 0:
                if end != 0 and n_days != 0:
                    end = pd.to_datetime(end).tz_localize(None).floor('D')
                    start = end - pd.Timedelta(days=n_days)
                else:
                    raise ValueError('Invalid user input')
            else:
                start = pd.to_datetime(start).tz_localize(None).floor('D')
                if end != 0 and n_days != 0:
                    end = pd.to_datetime(end).tz_localize(None).floor('D')
                    end = start + pd.Timedelta(days=n_days)
                elif end == 0 and n_days == 0:
                    raise ValueError('Invalid user input')
                else:
                    end = start + pd.Timedelta(days=n_days)
    
            return start, end
       else:
            raise ValueError('Invalid user input')

但是上面的代码不起作用.

But above code does not work.

推荐答案

发布不起作用"的代码时,包含错误跟踪或无法正常工作的细节非常有帮助.

When posting code that "doesn't work" it is extremely helpful to include the error trace or specifics about what isn't working.

也就是说,这是一个问题:

That said, this is a problem:

REQUIRED_KEYS = ["blue", "white"]
for bluewhite_category in REQUIRED_KEYS[0] or REQUIRED_KEYS[1]:
    #print(bluewhite_category)
    if bluewhite_category in REQUEST_OBJ.keys():
        param_obj_list = REQUEST_OBJ[bluewhite_category]

结构:

for x in a or b:

没有按照您的期望做.在列表或集合中使用 in 函数.

is not doing what you hoped for. Use the in function with a list or collection.

此外,未定义 REQUEST_OBJ .应该是该功能的输入吗?

Also, REQUEST_OBJ is not defined. Should it be an input to the function?

这篇关于迭代列表字典中的两个元素中的任何一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆