简体   繁体   English

在词典列表中更改相同的值

[英]Change identical values in a list of dictionaries

I am trying to pull specific data out relating to a specific code. 我正在尝试提取与特定代码有关的特定数据。 I managed to pull out the data into a new list of dictionaries but I need to changed the date format from 'Ymd H:M:S' to 'd/m/Y'. 我设法将数据提取到新的词典列表中,但是我需要将日期格式从“ Ymd H:M:S”更改为“ d / m / Y”。

data =  [
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.32'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.16'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.18'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.43'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.33'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.01'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.53'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.22'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.12'},
]

specific_code_data= []
def find_result_with_specific_code(x):
  global specific_code_data
  for d in test_data.ilab_data:
    if d['code'] == x:
        specific_code_data.append(d)
return specific_code_data

I tried writing something along the lines of: 我尝试按照以下方式写东西:

for d in specific_code_data:
    indate = d['collection_date_time']
    dt_obj = datetime.datetime.strptime(indate, '%Y-%m-%d %H:%M:%S')
    dt_str = datetime.datetime.strftime(dt_obj, '%d/%m/%Y')

But I am not sure how to change the value of the key collection_date_time in my list of dictionaries to the format I need. 但是我不确定如何将字典列表中的键collection_date_time的值更改为所需的格式。 Any help or pointing in the right direction would be greatly appreciated. 任何帮助或指出正确的方向将不胜感激。

I think you are missing a step here. 我想您在这里错过了一步。 You haven't re-assigned the modified string: 您尚未重新分配修改后的字符串:

for d in specific_code_data:
    indate = d['collection_date_time']
    dt_obj = datetime.datetime.strptime(indate, '%Y-%m-%d %H:%M:%S')
    dt_str = datetime.datetime.strftime(dt_obj, '%d/%m/%Y')
    d['collection_date_time'] = dt_str` # you need to do this

You might also try doing the date transformation with a different function as you go along: 您还可以尝试使用其他函数进行日期转换:

specific_code_data= []
def find_result_with_specific_code(x):
  global specific_code_data
  for d in test_data.ilab_data:
    if d['code'] == x:
        z = change_datetime_str(d) # line I added
        specific_code_data.append(z) # changed d to z
return specific_code_data

How about something like this: 这样的事情怎么样:

from datetime import datetime

data =  [
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.32'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.16'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.18'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.43'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.33'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.01'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.53'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.22'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.12'},
]

def alter_datetime_format(entry):
    collection_date_time = datetime.strptime(entry["collection_date_time"], "%Y-%m-%d %H:%M:%S")
    entry["collection_date_time"] = datetime.strftime(collection_date_time, "%d/%m/%Y")
    return entry

def find_result_with_specific_code(x, data):
    return [
        alter_datetime_format(entry)
        for entry in data
        if entry["code"] == x
    ]
'but I need to changed the date format from 'Y-m-d H:M:S' to 'd/m/Y'

i'll also jump in here... using pandas 我也会跳到这里...用pandas

data =  [
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.32'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.16'},
    {'collection_date_time': '2018-05-01 00:00:00', 'code': '96423', 'result': '0.18'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.43'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.33'},
    {'collection_date_time': '2017-12-24 13:04:00', 'code': '45674', 'result': '0.01'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.53'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.22'},
    {'collection_date_time': '2018-04-08 09:43:00', 'code': '12343', 'result': '0.12'},
]

sample = pd.DataFrame([d for d in data])

sample['collection_date_time'] = pd.to_datetime(sample['collection_date_time']).dt.normalize()
sample['collection_date_time'] = sample['collection_date_time'].dt.strftime("%d/%m/%Y")

data = []
for value in sample.values:
    list_of_dict = {}
    list_of_dict['code'] = value[0]
    list_of_dict['collection_date_time'] = value[1]
    list_of_dict['result'] = value[2]
    data.append(list_of_dict)

print(data)

[{'code': '96423', 'collection_date_time': '01/05/2018', 'result': '0.32'},
 {'code': '96423', 'collection_date_time': '01/05/2018', 'result': '0.16'},
 {'code': '96423', 'collection_date_time': '01/05/2018', 'result': '0.18'},
 {'code': '45674', 'collection_date_time': '24/12/2017', 'result': '0.43'},
 {'code': '45674', 'collection_date_time': '24/12/2017', 'result': '0.33'},
 {'code': '45674', 'collection_date_time': '24/12/2017', 'result': '0.01'},
 {'code': '12343', 'collection_date_time': '08/04/2018', 'result': '0.53'},
 {'code': '12343', 'collection_date_time': '08/04/2018', 'result': '0.22'},
 {'code': '12343', 'collection_date_time': '08/04/2018', 'result': '0.12'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在字典中查找具有相同值的嵌套列表[Python] - Finding nested list with identical values in Dictionaries [Python] 将相同的值与Python中的字典列表组合 - Combining identical values against a list of dictionaries in Python 将具有相同键但不同值的字典拆分到不同字典的列表 - Split list of dictionaries with identical keys but different values to different dictionaries 将相同字典的列表转换为数据框 - Convert list of identical dictionaries to Dataframe 将字典值从 python 中的字典列表更改为字典 - change dictionary values to dictionaries from list of dictionaries in python Python 使用来自 N 个具有相同条目但值不同的字典的值列表制作主字典 - Python make master dictonary with list of values from N dictionaries with identical entries but different values 将不同字典列表中的 Python Key 名称更改为另一个 Key 名称 - Change Python Key name in a list of non-identical dictionaries to another Key name 如何在两个词典中映射相同的项目并生成其相应键的值列表 - How to map identical items in two dictionaries and produce a list of values of their corresponding keys 在看似相同的字典中分配的值不同 - Values assigned differently in seemingly identical dictionaries 合并其值为字典的字典列表的值 - Merge values of list of dictionaries whose values are dictionaries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM