我有一个列表列表,我试图根据他们的项目对它们进行分组或聚类.如果上一组中没有元素,嵌套列表将启动一个新组.
输入:
paths = [
['D', 'B', 'A', 'H'],
['D', 'B', 'A', 'C'],
['H', 'A', 'C'],
['E', 'G', 'I'],
['F', 'G', 'I']]
我失败的代码:
paths = [
['D', 'B', 'A', 'H'],
['D', 'B', 'A', 'C'],
['H', 'A', 'C'],
['E', 'G', 'I'],
['F', 'G', 'I']
]
groups = []
paths_clone = paths
for path in paths:
for node in path:
for path_clone in paths_clone:
if node in path_clone:
if not path == path_clone:
groups.append([path, path_clone])
else:
groups.append(path)
print groups
预期产出:
[
[
['D', 'B', 'A', 'H'],
['D', 'B', 'A', 'C'],
['H', 'A', 'C']
],
[
['E', 'G', 'I'],
['F', 'G', 'I']
]
]
另一个例子:
paths = [['shifter', 'barrel', 'barrel shifter'],
['ARM', 'barrel', 'barrel shifter'],
['IP power', 'IP', 'power'],
['ARM', 'barrel', 'shifter']]
预期产出组:
output = [
[['shifter', 'barrel', 'barrel shifter'],
['ARM', 'barrel', 'barrel shifter'],
['ARM', 'barrel', 'shifter']],
[['IP power', 'IP', 'power']],
]
最佳答案 您正在基于集合进行分组,因此使用集合来检测新组:
def grouper(sequence):
group, members = [], set()
for item in sequence:
if group and members.isdisjoint(item):
# new group, yield and start new
yield group
group, members = [], set()
group.append(item)
members.update(item)
yield group
这给出了:
>>> for group in grouper(paths):
... print group
...
[['D', 'B', 'A', 'H'], ['D', 'B', 'A', 'C'], ['H', 'A', 'C']]
[['E', 'G', 'I'], ['F', 'G', 'I']]
或者您可以再次将其强制转换为列表:
output = list(grouper(paths))
这假设这些组是连续的.如果您有不相交的组,则需要处理整个列表并循环遍历为每个项目构建的所有组:
def grouper(sequence):
result = [] # will hold (members, group) tuples
for item in sequence:
for members, group in result:
if members.intersection(item): # overlap
members.update(item)
group.append(item)
break
else: # no group found, add new
result.append((set(item), [item]))
return [group for members, group in result]