我的程序生成如下列表:
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)
我一直在寻找一种可靠的方法来获取提醒。
答案 0 :(得分:3)
你可以试试这个:
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]
答案 1 :(得分:1)
如果值很大,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
答案 2 :(得分:0)
您可以使用sets
函数
symmetric_difference
进行操作
>>> 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'])
>>>