2017-04-18 77 views
-1

我有一个列表字典,我想根据最新日期获取值。如何获取数组或元素的最新值

例如:

a = { 
    "3456" :["031","a","10-09-1988","fff","xxx/xxx","032","b","20-10-1999","sss","yyy/yyyy"], 
    "5323" :["031","a","10-10-1999","fff","xxx/xxx","032","b","20-10-1988","sss","yyy/yyyy"], 
} 

上述词典有2项分别具有2个日期:

  • 32456 - > 1988年10月9日和20-10-1999 (在此20-10-1999是最新的,所以我需要yyy/yyy值作为ouptut)
  • 5323 - > 10-10-1999和20-10-1988 (在这10-10-1999是最新的,所以我需要xxx/xxx值作为ouptut)

我想下面这个脚本,但预期它不工作

lst = sorted(a , key=lambda x: datetime.strptime(x[2], "%Y-%m-%d")) 

new_dict = {(item[4]): item for item in lst} 

预期输出:

yyy/yyy 
xxx/xxx 
+1

首先尝试通过'for循环'和'if condition'获得预期输出,然后使用advance方法。 –

+0

'yyy/yyy'或者'xxx/xxx'来自大日期值的两个索引后的列表 –

+0

它是数组的元素,您可以在这里看到[“031”,“a”,“10-09-1988” ,“fff”,“xxx/xxx”, – Mounarajan

回答

0

通过正常的循环,如果条件

  1. 日期格式为day-month-year%d-%m-%Y,您正在检查"%Y-%m-%d"格式。
  2. 使用简单条件来比较两个日期值。

演示

>>> from datetime import datetime 
>>> a 
{'3456': ['031', 'a', '10-09-1988', 'fff', 'xxx/xxx', '032', 'b', '20-10-1999', 'sss', 'yyy/yyyy'], '5323': ['031', 'a', '10-10-1999', 'fff', 'xxx/xxx', '032', 'b', '20-10-1988', 'sss', 'yyy/yyyy']} 
>>> final_result = [] 
>>> for key in a: 
... if datetime.strptime(a[key][2], "%d-%m-%Y") > datetime.strptime(a[key][7], "%d-%m-%Y"): 
...  final_result.append(a[key][4]) 
... else: 
...  final_result.append(a[key][9]) 
... 
>>> final_result 
['yyy/yyyy', 'xxx/xxx'] 
>>> 
+0

您假设[key] [4]和[键] [9],但认为阵列将有更多的元素,我怎么能自动化它? – Mounarajan

+0

我给出了硬编码值(你给出的输入)的解决方案。现在你可以根据你的输入修改代码。 –

0

首先,当您使用asorted你得到的字典排序键的列表,因为当你遍历你遍历键的字典。然后,您尝试使用strptime作为关键功能,但这不起作用,因为您将索引编入字符串,如"3456",因此x[2]5。最后,你试着做一个new_dict,但我不知道你在期待什么,因为字典本质上是无序的。

如果我们可以假设您列表的形式总是相同,那么你不想列表值进行排序,真的。只需使用以下命令:

In [4]: def extract(x, ts="%d-%m-%Y"): 
    ...:  if datetime.strptime(x[2], ts) > datetime.strptime(x[7], ts): 
    ...:   return x[4] 
    ...:  else: 
    ...:   return x[9] 
    ...: 

现在你可以使用一个循环或理解:

In [5]: [extract(lst) for lst in a.values()] 
Out[5]: ['xxx/xxx', 'yyy/yyyy'] 

注意,你不能保证上述的顺序,因为字典是无序

除非你想遍历排序键:

In [21]: [extract(a[k]) for k in sorted(a) ] 
Out[21]: ['yyy/yyyy', 'xxx/xxx'] 

但目前还不清楚,如果这是你想要的。

+0

你假设一个[键] [4]和一个[键] [9],但认为该阵列将有更多的元素,我怎么能自动化它? – Mounarajan

+0

@Mounarajan它是一个*列表*,而不是一个数组。但是你有任何控制权吗?这是数据结构的糟糕选择。你原来的帖子暗示这些列表会有这种形式,但你的意思是它可能有更多的元素?你知道任何关于列表结构的东西吗? –

0

建议您先从列表中构造数据,然后比较日期,最后得到结果。

from _datetime import datetime 


data = { 
    "3456": ["031", "a", "10-09-1988", "fff", "xxx/xxx", "032", "b", "20-10-1999", "sss", "yyy/yyyy"], 
    "5323": ["031", "a", "10-10-1999", "fff", "xxx/xxx", "032", "b", "20-10-1988", "sss", "yyy/yyyy"] 
} 

obj_elements_num = 5 
date_format = '%d-%m-%Y' 


def string_to_date_object(date_string): 
    return datetime.strptime(date_string, date_format) 


for a_dict in data: 
    tmp_date, tmp_value, tmp_len = "01-01-1970", "TMP", 0 
    while tmp_len < len(data[a_dict]): 
     _, _, cmp_date, _, value = data[a_dict][tmp_len:tmp_len+obj_elements_num] 
     if string_to_date_object(cmp_date) > string_to_date_object(tmp_date): 
      tmp_date, tmp_value = cmp_date, value 
     tmp_len += obj_elements_num 
    print(tmp_date, tmp_value) 

---------------------------------------------------------------- 
20-10-1999 yyy/yyyy 
10-10-1999 xxx/xxx 
相关问题