【问题标题】:adding two dictionaries that are already inside same list添加两个已经在同一个列表中的字典
【发布时间】:2020-03-08 08:27:42
【问题描述】:

我有一列 50983 行。每行都有一个列表,其中有两个或多个字典。我想在一本字典中制作所有字典。我想在每个词典中更新这个 id。我用过:

l=[{'id':'abc12vr'},{'createdAt': '2018-12-18T16:09:57.098Z',
  'notes': 'Candidate initial submission.',
  'createdBy': 'Steven Klinger'},
 {'createdAt': '2018-12-18T23:14:09.415Z',
  'notes': 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>',
  'createdBy': 'Matt'},
 {'createdAt': '2019-01-22T16:04:46.958Z',
  'notes': 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>',
  'createdBy': 'Matt'},
 {'createdAt': '2018-12-18T16:09:57.098Z',
  'notes': 'Candidate initial submission.',
  'createdBy': 'Steven Klinger'},
 {'createdAt': '2018-12-18T23:14:09.415Z',
  'notes': 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>',
  'createdBy': 'Matt'},
 {'createdAt': '2019-01-22T16:04:46.958Z',
  'notes': 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>',
  'createdBy': 'Matt'}]

id_dict = [d for d in l if 'id' in d][0]
merge = [{**d,**id_dict} for d in l if 'id' not in d]

但是我只得到最后一行的字典,我想要每一行

【问题讨论】:

  • 它应该是什么样子?在您的列表中,您有具有相同键的字典。你怎么能把它放在一个字典里?

标签: python pandas list dictionary


【解决方案1】:

似乎this 的答案应该会有所帮助(虽然不确定,因为您没有提供所需的输出):

d = {}
for i in l:
    for k in i.keys():
        d[k] = list(d[k] for d in l)

{'createdAt': ['2018-12-18T16:09:57.098Z', '2018-12-18T23:14:09.415Z', '2019-01-22T16:04:46.958Z', '2018-12-18T16:09:57.098Z', '2018-12-18T23:14:09.415Z', '2019-01-22T16:04:46.958Z'], 'notes': ['Candidate initial submission.', 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>', 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>', 'Candidate initial submission.', 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>', 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>'], 'createdBy': ['Steven Klinger', 'Matt', 'Matt', 'Steven Klinger', 'Matt', 'Matt']}

【讨论】:

  • 但是没有id,哦我忘了告诉你在另一列有id需要在这个字典里
  • @AamerAshfaque 在这种情况下你应该更新你的问题
  • @politicalscientist 这真的太低效了。对于每个字典,您再次传递整个列表。它至少是 O(n^2) 并且对于中等大小的列表会很糟糕
  • @political 科学家,请立即查看
【解决方案2】:

这会遍历数据:

from collections import defaultdict

output_dict = defaultdict(list)

for d in l:
    for key in d:
        output_dict[key].append(d[key])

>>> output

defaultdict(list,
            {'createdAt': ['2018-12-18T16:09:57.098Z',
              '2018-12-18T23:14:09.415Z',
              '2019-01-22T16:04:46.958Z',
              '2018-12-18T16:09:57.098Z',
              '2018-12-18T23:14:09.415Z',
              '2019-01-22T16:04:46.958Z'],
             'notes': ['Candidate initial submission.',
              'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>',
              'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>',
              'Candidate initial submission.',
              'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>',
              'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>'],
             'createdBy': ['Steven Klinger',
              'Matt',
              'Matt',
              'Steven Klinger',
              'Matt',
              'Matt']})

【讨论】:

    【解决方案3】:

    原答案

    我假设您需要一个键和该键的所有值才能附加到一个列表中。这里我使用了dictionarysetdefault方法来实现。

    # Input
    l=[{'createdAt': '2018-12-18T16:09:57.098Z',
      'notes': 'Candidate initial submission.',
      'createdBy': 'Steven Klinger'},
     {'createdAt': '2018-12-18T23:14:09.415Z',
      'notes': 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>',
      'createdBy': 'Matt'},
     {'createdAt': '2019-01-22T16:04:46.958Z',
      'notes': 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>',
      'createdBy': 'Matt'},
     {'createdAt': '2018-12-18T16:09:57.098Z',
      'notes': 'Candidate initial submission.',
      'createdBy': 'Steven Klinger'},
     {'createdAt': '2018-12-18T23:14:09.415Z',
      'notes': 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>',
      'createdBy': 'Matt'},
     {'createdAt': '2019-01-22T16:04:46.958Z',
      'notes': 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>',
      'createdBy': 'Matt'}]
    
    # Main code
    res = {} # defined output dict
    for i in l: # for loop to fetch each element(dict) inside a list
        for k, v in i.items(): # to fetch key value fair of each dict
            res.setdefault(k, []).append(v) # setdefault method of add key to result and created an empty list and appended value to it.  
    print (res) # print result
    
    # Output
    # {'createdAt': ['2018-12-18T16:09:57.098Z', '2018-12-18T23:14:09.415Z', '2019-01-22T16:04:46.958Z', '2018-12-18T16:09:57.098Z', '2018-12-18T23:14:09.415Z', '2019-01-22T16:04:46.958Z'], 'notes': ['Candidate initial submission.', 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>', 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>', 'Candidate initial submission.', 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>', 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>'], 'createdBy': ['Steven Klinger', 'Matt', 'Matt', 'Steven Klinger', 'Matt', 'Matt']}
    

    修改后的答案

    # NOTE: "l" is individual list of the your data set.
    value_for_id = "abc" # Value to be set for id
    for i in l: # For each element in l - where l is your individual list
        if i.get("id",None) is not None: # verify if dict with key -> "id" exist
            i["id"] = value_for_id # If exist then update the value for key -> "id"
            break # break and come out of the for loop
    else: # if there is no break, i.e. data doesn't have dict with "id" then we will append a new dict to the list. 
        l.append({"id":value_for_id}) # Appending new dict to the list
    
    print (l)
    

    我希望这会有所帮助并很重要!

    【讨论】:

    • 奇怪!,您确定您使用的输入数据与此处询问的相同吗?您的问题只有一个列表分配给l。如果您的输入数据有多个列表,则可能会发生上述错误,在这种情况下,请在for i in l:上方再使用一个for循环
    • 我只需要用 'id' 更新字典:在同一个列表中的值字典。是的,我在每一行中列出了 50983 个列表。我刚刚发布了我的专栏的第一行
    • 好的,所以你已经更新了问题。根据您更新的问题,我编辑了我的答案。请仔细阅读它和所有其他答案。如果您的问题得到解决,我请求您关闭此问题。
    【解决方案4】:

    这是我在stackflow中的首次回答,希望对你有所帮助!

    你只得到一个字典的最后一行,我想要每一行 - 因为字典必须有一个唯一的键,而且字典中的所有键都是相同的,这就是 python 不断覆盖键的地方。

    下面的代码会将所有字典合并为一个,并在键中附加一个计数器值以使键唯一。

    merged_dict={}
    counter=0
    def merge_logic(dict_para):
        #print dict_val
        global counter
        for key,value in dict_para.items():    
            merged_dict[key+"_"+str(counter)]=value
            counter+=1
    id_dict = [merge_logic(d) for d in l if isinstance(d,dict)]
    
    print merged_dict
    

    输出:

        {'createdAt_11': '2018-12-18T16:09:57.098Z', 
    'notes_0': 'Candidate initial submission.', 
    'notes_3': 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>', 
    'createdAt_14': '2018-12-18T23:14:09.415Z', 
    'createdAt_17': '2019-01-22T16:04:46.958Z', 
    'notes_6': 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>', 
    'notes_9': 'Candidate initial submission.', 
    'createdBy_13': 'Matt', 
    'notes_12': 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>', 
    'createdAt_5': '2018-12-18T23:14:09.415Z', 
    'notes_15': 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>', 
    'createdAt_2': '2018-12-18T16:09:57.098Z', 
    'createdBy_4': 'Matt', 
    'createdBy_7': 'Matt', 
    'createdBy_1': 'Steven Klinger', 
    'createdAt_8': '2019-01-22T16:04:46.958Z', 
    'createdBy_10': 'Steven Klinger', 
    'createdBy_16': 'Matt'}
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多