我有一个带有字符串的列表清单:
list = [["a","b"],["c","d"],["a", "e"],["f","d"],["x","y"]]
现在我要合并所有列表,其中有1个类似项,如下所示:
grouped_list = [["a", "b", "e"],["c","d","f"],["x","y"]]
我的代码现在是这样:
list = [["a","b"],["b","c"],["d","e"],["x","y"]]
clist = list.copy()
result = []
counter = 0
del_list = []
def oneofsame(L1, L2):
counter = 0
for i in L1:
for j in L2:
if i == j:
counter += 1
if counter == 0:
return False
else:
return True
for l in list:
try:
del clist[clist.index(l)]
except:
pass
result.append([])
for i in l:
for cl in clist:
if oneofsame(l, cl):
for j in l:
if j not in result[counter]:
result[counter].append(j)
for j in cl:
if j not in result[counter]:
result[counter].append(j)
del_list.append(cl)
else:
result.append(cl)
del_list.append(cl)
for j in del_list:
del clist[clist.index(j)]
del_list = []
counter += 1
del_list = []
cresult = result.copy()
for i in range(len(cresult)-1, 0, -1):
if cresult[i] == []:
del result[i]
print(result)
但是此代码不会合并所有示例输入(我无法粘贴示例输入,因为它的敏感数据)
答案 0 :(得分:1)
这是一种方法。
对于每对:
def group_equals(lst):
groups = []
for pair in lst:
pair = set(pair)
equals_found = 0
for idx, group in enumerate(groups):
if group.intersection(pair):
equals_found += 1
if equals_found == 1:
# We found a first group that contains one of our values,
# we can add our pair to the group
group.update(pair)
first_group = group
elif equals_found == 2:
# We found a second group that contains the other one of
# our values, we merge it with the first one
first_group.update(group)
del groups[idx]
break
# If none of our values was found, we create a new group
if not equals_found:
groups.append(pair)
return [list(sorted(group)) for group in groups]
tests = [ [["a", "b"], ["c", "d"], ["b", "c"]], # all equal
[["a","b"],["c","d"],["a", "e"],["f","d"]],
[["a","b"],["c","d"],["a", "e"],["f","d"],["x","y"]]
]
for lst in tests:
print(group_equals(lst))
# [['a', 'b', 'c', 'd']]
# [['a', 'b', 'e'], ['c', 'd', 'f']]
# [['a', 'b', 'e'], ['c', 'd', 'f'], ['x', 'y']]
答案 1 :(得分:0)
希望下面的代码能解决您的问题:
import itertools
import copy
lista = [["a","b"],["c","d"],["a", "e"],["f","d"],["x","y"]] #[["a","b"],["e","d1"],["a", "e"],["a","d"],["d","y"]]
def grouped_list(lista):
aa = []
bbc = copy.deepcopy(lista)
flag = False
for a, b in itertools.combinations(lista,2):
bb = a+b
if len(set(bb)) < len(bb):
flag = True
cc = list(set(bb))
cc.sort()
if cc not in aa: aa.append(cc)
if a in lista: lista.remove(a)
if b in lista: lista.remove(b)
if lista: aa = aa + lista
if not flag: return bbc
else: return grouped_list(aa)
print ("Grouped list -->", grouped_list(lista))
可以随意询问/建议上面的代码中的任何内容。