问题:
我有一个非常简单的问题,如果两个中的任何一个都不为空,为空或不存在,并且两个都执行,则需要将一个日期(origDate或destDate)更新为另一个日期(origDate或destDate)。不存在,然后将两者都设置为“无”。
我正在寻找一种实现此目标的Python方法。我当前的代码工作正常,但是很冗长
示例输入:
1. origin_dest_date = {
"originDate": "2019-06-30 23:59:00.000",
"destDate": None
}
2. origin_dest_date = {
"originDate": "2019-06-30 23:59:00.000"
}
3. origin_dest_date = {
"originDate": ""
}
输出:
1. origin_dest_date = {
"originDate": "2019-06-30 23:59:00.000",
"destDate": "2019-06-30 23:59:00.000"
}
2. origin_dest_date = {
"originDate": "2019-06-30 23:59:00.000",
"destDate": "2019-06-30 23:59:00.000"
}
我的代码:
from dateutil.parser import parse
origin_dest_date = {
"originDate": "2019-06-30 23:59:00.000",
"destDate": None
}
isoriginDate = False
isdestDate = False
if 'originDate' in origin_dest_date:
isoriginDate = True
if origin_dest_date['originDate'] is not None and \
not origin_dest_date['originDate'] == '':
parse(origin_dest_date['originDate'])
if origin_dest_date['originDate'] == '':
origin_dest_date['originDate'] = None
if 'destDate' in origin_dest_date:
isdestDate = True
if origin_dest_date['destDate'] is not None and \
not origin_dest_date['destDate'] == '':
parse(origin_dest_date['destDate'])
if origin_dest_date['destDate'] == '':
origin_dest_date['destDate'] = None
if isoriginDate and not isdestDate:
origin_dest_date['destDate'] = origin_dest_date['originDate']
elif not isoriginDate and isdestDate:
origin_dest_date['originDate'] = origin_dest_date['destDate']
elif isoriginDate and origin_dest_date['originDate'] is None and \
isdestDate and \
origin_dest_date['destDate'] is not None:
origin_dest_date['originDate'] = origin_dest_date['destDate']
elif isdestDate and \
origin_dest_date['destDate'] is None and \
isoriginDate and \
origin_dest_date['originDate'] is not None:
origin_dest_date['destDate'] = origin_dest_date['originDate']
elif not isoriginDate and not isdestDate:
origin_dest_date['originDate'] = None
origin_dest_date['destDate'] = None
print(origin_dest_date)
答案 0 :(得分:1)
代码中有两点需要改进:
使用变量使它看起来更整洁,尤其是条件语句,因此您不必重复访问字典中的相同值;
使用字典get
方法查找键-如果键存在,则返回其对应的值,否则返回None
。然后只需检查返回的值在Python中是否为真,该值同时说明了None
和空字符串。在末尾设置值时,也可以使用setdefault
。
这是一个如何实现这些事情以简化代码的示例:
from dateutil.parser import parse
origin_dest_date = { "originDate": "2019-06-30 23:59:00.000", "destDate": None }
origin = origin_dest_date.get("originDate")
dest = origin_dest_date.get("destDate")
if origin and not dest:
origin_dest_date["destDate"] = parse(origin)
elif not origin and dest:
origin_dest_date["originDate"] = parse(dest)
elif not origin and not dest:
if origin_dest_date.setdefault("originDate", None) is not None: # conditional for empty string
origin_dest_date["originDate"] = None
if origin_dest_date.setdefault("destDate", None) is not None: # conditional for empty string
origin_dest_date["destDate"] = None
答案 1 :(得分:1)
这是我的尝试:
def fill_dict(origin_dest_date, key1, key2):
if not origin_dest_date.get(key1):
origin_dest_date[key1] = origin_dest_date.get(key2) or None
fill_dict(origin_dest_date, 'destDate', 'originDate')
fill_dict(origin_dest_date, 'originDate', 'destDate')
关键思想是:
None
if
语句。空字符串或None
(如果键在字典中不存在,就是这种情况)将解析为False
。答案 2 :(得分:1)
具有自定义功能和示例模板dict:
def arrange_date_keys(d):
temp = {'originDate': None, 'destDate': None} # sample dict
if not d.get('originDate') or not d.get('destDate'):
date_value = d.get('originDate') or d.get('destDate')
return temp.fromkeys(temp, date_value)
return d
origin_dest_date1 = {
"originDate": "2019-06-30 23:59:00.000", "destDate": None
}
origin_dest_date2 = {
"originDate": "2019-06-30 23:59:00.000"
}
origin_dest_date3 = {
"originDate": ""
}
origin_dest_date4 = {
"originDate": "2019-06-30 23:59:00.000", "destDate": "2019-06-30 23:59:00.000"
}
print(arrange_date_keys(origin_dest_date1))
print(arrange_date_keys(origin_dest_date2))
print(arrange_date_keys(origin_dest_date3))
print(arrange_date_keys(origin_dest_date4))
连续输出:
{'originDate': '2019-06-30 23:59:00.000', 'destDate': '2019-06-30 23:59:00.000'}
{'originDate': '2019-06-30 23:59:00.000', 'destDate': '2019-06-30 23:59:00.000'}
{'originDate': None, 'destDate': None}
{'originDate': '2019-06-30 23:59:00.000', 'destDate': '2019-06-30 23:59:00.000'}
答案 3 :(得分:1)
尝试一下
EXPLAIN
最终结构,默认值为type
,该函数将更新该结构。
ALL
测试:
ref
答案 4 :(得分:0)
更新:
try:
if 'destDate' not in origin_dest_date:
origin_dest_date['destDate'] = origin_dest_date['originDate']
elif not origin_dest_date['destDate']:
origin_dest_date['destDate'] = origin_dest_date['originDate']
except KeyError as e:
pass
print(origin_dest_date)
答案 5 :(得分:0)
如果只需要两个静态键,则可以尝试两行代码
must = {"originDate", "destDate"}
test = {"originDate":"2019-06-30 23:59:00.000", "destDate": None}
test.update({key:test[list(test.keys()-key)[0]] for key in must if key not in test or test[key] is None})
尝试了一个None值:
test = {"originDate":"2019-06-30 23:59:00.000", "destDate": None}
test.update({key:test[list(test.keys()-key)[0]] for key in must if key not in test or test[key] is None})
test
{'originDate': '2019-06-30 23:59:00.000', 'destDate': '2019-06-30 23:59:00.000'}
尝试过一个键:
test = {"originDate":"2019-06-30 23:59:00.000",}
test.update({key:test[list(test.keys()-key)[0]] for key in must if key not in test or test[key] is None})
test
{'originDate': '2019-06-30 23:59:00.000', 'destDate': '2019-06-30 23:59:00.000'}
答案 6 :(得分:0)
这是@deceze
提供的最准确的解决方案origin_dest_date = {
"originDate": "2019-06-30 23:59:00.000",
"destDate": "2019-06-30 23:58:00.000"
}
if not origin_dest_date.get('originDate') or not origin_dest_date.get('destDate'):
d = (origin_dest_date.get('originDate') or None) or (origin_dest_date.get('destDate') or None)
origin_dest_date = {'originDate': d, 'destDate': d}
print(origin_dest_date)