Python嵌套列表内部比较和编辑
我一直在想办法,最简单的解释方法是用一个例子:Python嵌套列表内部比较和编辑,python,list,python-3.x,Python,List,Python 3.x,我一直在想办法,最简单的解释方法是用一个例子: a = [[1, 2, 4], [2, 5], [0, 3, 7, 8], [12, 3, 6], [18, 14]] 这就是我开始列出的清单。 我需要得到一个列表,其中包含一个列表中所有列表的列表,这些列表包含添加在一起的重叠元素 result = [[1, 2, 4, 5], [0, 3, 6, 7, 8, 12], [14, 18]] 我该怎么办 亲切问候,, das我可以想到的一种方法是通过递归。从一个项目开始,然后循环,直到找到它连接
a = [[1, 2, 4], [2, 5], [0, 3, 7, 8], [12, 3, 6], [18, 14]]
这就是我开始列出的清单。
我需要得到一个列表,其中包含一个列表中所有列表的列表,这些列表包含添加在一起的重叠元素
result = [[1, 2, 4, 5], [0, 3, 6, 7, 8, 12], [14, 18]]
我该怎么办
亲切问候,,
das我可以想到的一种方法是通过递归。从一个项目开始,然后循环,直到找到它连接的每个数字。对于这些数字中的每一个,您必须执行相同的操作。因此递归。为了提高效率,请将您访问过的数字存储在列表中,并在每个递归序列的开头检查,以确保您不会重复任何探索。两行:
a_set = [set(x) for x in a]
result = [list(x.union(y)) for i,x in enumerate(a_set) for y in a_set[i:]
if x.intersection(y) and x != y]
这将逐个遍历列表,并从当前子列表(s
)创建一个集合。然后,如果有另一个集合t
与其具有非空交点,它将检查结果。如果是这种情况,则将s
中的项目添加到该集合t
。如果没有具有非空交叉点的t
,则s
是一个新的独立结果,可以附加到结果列表中
这样的问题也是一个很好的例子。在本例中,您将查看列表并继续合并子列表,只要您仍然可以找到重叠的列表。您可以使用查看成对的子列表来实现此功能:
result = [set(x) for x in a] # start with the original list of sets
fixedPoint = False # whether we found a fixed point
while not fixedPoint:
fixedPoint = True
for x, y in combinations(result, 2): # search all pairs …
if x & y: # … for a non-empty intersection
x.update(y)
result.remove(y)
# since we have changed the result, we haven’t found the fixed point
fixedPoint = False
# abort this iteration
break
我给你留下了最后一步:
a = [[1, 2, 4], [2, 5], [0, 3, 7, 8], [12, 3, 6], [18, 14]]
result = [[1, 2, 4, 5], [0, 3, 6, 7, 8, 12], [14, 18]]
# each sub list
result2 = []
count = 0
print a
for sub_list in a:
print count
print "sub_list: " + str(sub_list)
a.pop(count)
print "a: " + str(a)
#each int
sub_list_extend_flag = False
for int_in_sub_list in sub_list:
print "int_in_sub_list: " + str(int_in_sub_list)
for other_sub_list in a:
print "current_other_sub_list: " + str(other_sub_list)
if int_in_sub_list in other_sub_list:
sub_list_extend_flag = True
other_sub_list.extend(sub_list)
result2.append(list(set(other_sub_list)))
if not sub_list_extend_flag:
result2.append(sub_list)
count += 1
print result2
简单回答:
a = [[1, 2, 4], [2, 5], [0, 3, 7, 8], [12, 3, 6], [18, 14]]
for x in a:
for y in x:
print y
它比第一个更简单:
box=[]
a = [[1, 2, 4], [2, 5], [0, 3, 7, 8], [12, 3, 6], [18, 14]]
for x in a:
for y in x:
box.append(y)
print box
结果:[1,2,4,2,5,0,3,7,8,12,3,6,18,14]
与此相比,您可以比较以下数字:
box=[]
box2=""
a = [[1, 2, 4], [2, 5], [0, 3, 7, 8], [12, 3, 6], [18, 14]]
for x in a:
for y in x:
box.append(y)
print box
for a in box:
box2+=str(a)
print box2
结果:12425037812361814
您还可以让它更可爱:
print " ".join(box2)
结果:1 2 4 2 5 0 3 7 8 1 2 3 6 1 8 1 4是否可以将所有存在的子列表存储为列表?子列表的数量事先未知。看待此问题的一种方法(可能不必要地复杂化)是查找二部图的连接组件,一组节点是
a
的子列表,另一组是它们的条目。@qqvc(via):请不要编辑包含答案的问题。将其作为答案发布。这会删除{14,18}
,因为它与任何其他内容都不相交。这对列表列表也不起作用,因为每个列表有两个以上的列表重叠。例如[[1,2],[2,3],[3,4]
@poke,为什么要包括{14,18}
?问题是“一个列表,其中包含一个列表中所有列表的列表,这些列表包含叠加在一起的重叠元素”{14,18}
与另一个子列表没有任何重叠元素。如果您查看OP的示例,您可以清楚地看到,[14,18]
包含在所需的输出中。结果应该是来自输入的所有列表,但是在有重叠元素的地方,这些子列表组合在一起。这为我提供了[[1,2,4,5],[1,2,4,5],[0,3,6,7,8,12],[0,3,6,7,8,12],[0,3,6,7,8,12],[18,14]
,这不是期望的结果。忽略结果中的重复列表,这也不适用于两个以上或没有重叠的子列表的列表。例如[[1,2],[2,3],[3,4],[5,6]]
。
print " ".join(box2)