繁体   English   中英

Python:基于项目对将列表分组

[英]Python: group a list basing on pairs of items

我的程序生成如下列表:

mydata = ["foo", "bar", "baz", "quux", "quid", "quo"]

从其他数据中我知道,这些可以成对分组(这里是元组列表,但可以更改为任何形式):

static_mapping = [("foo", "quo"), ("baz", "quux"), ("quid", "bar")]

夫妻俩没有订餐。

现在开始解决问题:我的程序生成mydata ,我需要将数据分组但是要保留不匹配项的单独列表。 原因是,在任何时候, mydata可能都不会包含夫妇中的所有项目。

此类假设功能的预期结果:

 mydata = ["foo", "bar", "quo", "baz"]
 couples, remainder = group_and_split(mydata, static_mapping)
 print(couples)
 [("foo", "quo")]
 print(remainder)
 ["bar", "baz"]

编辑:我尝试过的示例(但他们停止寻找耦合):

found_pairs = list()
for coupling in static_mapping:
    pairs = set(mydata).intersect(set(coupling))
    if not pairs or len(pairs) != 2:
        continue
    found_pairs.append(pairs)

我被困在寻找一种可靠的方式来发出提醒。

您可以尝试以下方法:

import copy

def group_and_split(mydata, static_mapping):
    remainder = copy.deepcopy(mydata)
    couples = []
    for couple in static_mapping:
        if couple[0] in mydata and couple[1] in mydata:
            remainder.remove(couple[0])
            remainder.remove(couple[1])
            couples.append(couple)
    return [couples, remainder]

如果值很大,Set可以让您更快地运行,但是会占用内存,并且Deepcopy会保持原始数据完整。

假设功能的实现之一可能是:

from copy import deepcopy

def group_and_split(mydata, static_mapping):
        temp  = set(mydata)
        couples = []
        remainder = deepcopy(mydata)
        for value1,value2 in static_mapping:
                if value1 in temp and value2 in temp:
                        couples.append((value1,value2))
                        remainder.remove(value1)
                        remainder.remove(value2)
        return couples, remainder

在这里,您将如何使用symmetric_difference函数对sets进行操作:

>>> full =  ["foo", "quo", "baz", "quux", "quid", "bar", 'newone', 'anotherone']
>>> couples = [("foo", "quo"), ("baz", "quux"), ("quid", "bar")]

## now for the remainder. Note you have to flatten your couples:
>>> set(full).symmetric_difference([item for sublist in couples for item in sublist])
set(['anotherone', 'newone'])
>>> 

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM