简体   繁体   English

如何根据某些条件将字典列表拆分为单独的字典列表?

[英]How can I split a list of dictionaries in separate lists of dictionaries based on some condition?

I am new to python, and I am trying to split a list of dictionaries into separate lists of dictionaries based on some condition.我是 python 的新手,我正在尝试根据某些条件将字典列表拆分为单独的字典列表。

This is how my list looks like this:这就是我的列表的样子:

[{'username': 'AnastasiadesCY',
  'created_at': '2020-12-02 18:58:16',
  'id': 1.33421029132062e+18,
  'language': 'en',
  'contenttype': 'text/plain',
  'content': 'Pleased to participate to the international conference in support of the Lebanese people. Cypriot citizens, together with the Government 🇨🇾, have provided significant quantities of material assistance, from the day of the explosion until today.\n\n#Lebanon 🇱🇧'},
 {'username': 'AnastasiadesCY',
  'created_at': '2020-11-19 18:13:06',
  'id': 1.32948788307022e+18,
  'language': 'en',
  'contenttype': 'text/plain',
  'content': '#Cyprus stand ready to support all efforts towards a coordinated approach of vaccination strategies across Europe, that will prove instrumental in our fight against the pandemic.\n\nUnited Against #COVID19 \n\n#EUCO'},...

I would like to split and group all list's elements that have the same username into separate lists of dictionaries.我想将具有相同用户名的所有列表元素拆分并分组到单独的字典列表中。 The elements of the list - so each dictionary - are ordered by username.列表的元素 - 所以每个字典 - 按用户名排序。

Is there a way to loop over the dictionaries and append each element to a list until username in "item 1" is equal to username in "item 1 + 1" and so on?有没有办法遍历字典和 append 每个元素到一个列表,直到“项目 1”中的用户名等于“项目 1 + 1”中的用户名等等?

Thank you for your help!谢谢您的帮助!

A better would be to create a dictionary with username as key and value as list of user attributes更好的方法是创建一个字典,其中username作为键,值作为用户属性列表

op = defauldict(list)
for user_dic in list_of_userdictss:
    op[user_dic.pop('username')].append(user_dic)
op = OrderedDict(sorted(user_dic.items()))

Finding the same thing works the best if we sort the list by it - then all the same names are next to each other.如果我们按它对列表进行排序,则找到相同的东西效果最好——然后所有相同的名称都彼此相邻。

But even after sorting, we don't need to do such things manually - there are already tools for that.但即使在排序之后,我们也不需要手动做这些事情——已经有工具可以做到这一点。 :) - itertools.groupby documentation and a nice explanation how it works :) - itertools.groupby 文档一个很好的解释它是如何工作的

from itertools import groupby
from operator import itemgetter

my_list.sort(key=itemgetter("username"))
result = {}
for username, group in groupby(my_list, key=itemgetter("username")):
   result[username] = list(group)

result is a dict with usernames as keys result是一个以用户名作为键的字典

If you want a list-of-lists, do result = [] and then result.append(list(group)) instead.如果您想要一个列表列表,请执行result = []然后result.append(list(group))

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

相关问题 主要根据列表大小但其次根据条件在单独列表中拆分字典列表 - Split list of dictionaries in separate lists based primarily on list size but secondarily based on condition 带条件的字典嵌套列表 - Nested lists with dictionaries based on condition 将列表字典拆分为字典列表 - Split dictionary of lists into list of dictionaries 如何将字典列表转换为列表列表? - How can I convert list of dictionaries to list of lists? 如何根据Python中的特定条件拆分列表? - How can I split a list of lists based on a specific condition in Python? 如何将包含列表的 python 字典拆分为单独的字典 - How to split a python dictionary containing lists into separate dictionaries 如何检查条件的值? 它们在字典列表中,字典在列表中 - how can I check the value for a condition? They are in the list of dictionaries and the dictionary is in the list 我如何获取字典列表并根据键值将它们拆分 - How can I take a list of dictionaries and split them up based on key values python根据条件将字典列表分为两个列表 - python split list of dictionaries into two lists based on conditional 如何将具有多个文本列表值的python字典拆分为具有相同值的键的单独字典? - How can I split a python dictionary with multiple text-list values to have separate dictionaries of keys that have the same values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM