简体   繁体   English

根据列表中的另一个元素追加列表,并删除包含项的列表

[英]Append list based on another element in list and remove lists that contained the items

Let's say I have two lists like this: 假设我有两个这样的列表:

list_all = [[['some_item'],'Robert'] ,[['another_item'],'Robert'],[['itemx'],'Adam'],[['item2','item3'],'Maurice]]

I want to combine the items together by their holder (ie 'Robert') only when they are in separate lists. 我只想将它们的所有者(即“罗伯特”)放在单独的列表中,将它们组合在一起。 Ie in the end list_all should contain: 即,list_all结尾应包含:

list_all = [[['some_name','something_else'],'Robert'],[['itemx'],'Adam'],[['item2','item3'],'Maurice]]

What is a fast and effective way of doing it? 什么是快速有效的方法? I've tried in different ways but I'm looking for something more elegant, more simplistic. 我尝试了不同的方法,但是我正在寻找更优雅,更简单的东西。 Thank you 谢谢

Here is one solution. 这是一个解决方案。 It is often better to store your data in a more structured form, eg a dictionary, rather than manipulate from one list format to another. 通常最好以结构化的形式(例如字典)存储数据,而不是从一种列表格式转换为另一种列表格式。

from collections import defaultdict

list_all = [[['some_item'],'Robert'],
            [['another_item'],'Robert'],
            [['itemx'],'Adam'],
            [['item2','item3'],'Maurice']]

d = defaultdict(list)

for i in list_all:
    d[i[1]].extend(i[0])

# defaultdict(list,
#             {'Adam': ['itemx'],
#              'Maurice': ['item2', 'item3'],
#              'Robert': ['some_item', 'another_item']})

d2 = [[v, k] for k, v in d.items()]

# [[['some_item', 'another_item'], 'Robert'],
#  [['itemx'], 'Adam'],
#  [['item2', 'item3'], 'Maurice']]

You can try this, though it's quite similar to above answer but you can do this without importing anything. 您可以尝试执行此操作,尽管它与上述答案非常相似,但是您可以执行此操作而无需导入任何内容。

list_all = [[['some_item'], 'Robert'], [['another_item'], 'Robert'], [['itemx'], 'Adam'], [['item2', 'item3'], 'Maurice']]

x = {}  # initializing a dictionary to store the data

for i in list_all:
    try:
        x[i[1]].extend(i[0])
    except KeyError:
        x[i[1]] = i[0]

list2 = [[j, i ] for i,j in x.items()]
list_all = [[['some_item'],'Robert'] ,[['another_item'],'Robert'],[['itemx'],'Adam'],[['item2','item3'],'Maurice']]

dict_value = {}
for val in list_all:
    list_, name = val
    if name in dict_value:
        dict_value[name][0].extend(list_)
    else:      
        dict_value.setdefault(name,[list_, name])
print(list(dict_value.values()))
>>>[[['some_item', 'another_item'], 'Robert'],
 [['itemx'], 'Adam'],
 [['item2', 'item3'], 'Maurice']]

暂无
暂无

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

相关问题 从另一个列表列表中的项目检查列表列表中的元素 - Check element in list of lists from items in another list of lists 如何根据嵌套列表中的元素删除列表列表的重复项 - How to remove duplicates of list of lists based on element in nested list append 如果满足条件,则另一个嵌套列表中的嵌套列表的最后一个元素 - append last element of nested lists in another nested list if condition met 从基于另一个列表的列表列中删除列表值 - Remove list values from a column of lists based on another list 拆分列表项并删除拆分项(如果两者均未包含在Python的另一个列表中) - Split list item and remove split items if both are not contained in another list in Python 有没有更有效的方法可以根据另一个列表从列表中删除项目? - Is there a more efficient way to remove items from a list based on another list? 根据列表中的相似项检查列表的索引,然后根据列表的维度将其追加到另一个列表中 - checking the index of a list according to similar items within the list and then append into another list based on the dimension of the list 根据另一个列表列表对列表列表进行排序 - Sorting list of lists based on another list of lists 根据另一个列表列表过滤列表列表 - Filter list of lists based on another list of lists 如果元素包含在列表中,则从列表中删除元组 - remove tuple from list if element is contained within it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM