Python嵌套列表内部比较和编辑

时间:2014-11-28 01:53:39

标签: 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]]

我该怎么做?

亲切的问候, Daquicker

5 个答案:

答案 0 :(得分:5)

a = [[1, 2, 4], [2, 5], [0, 3, 7, 8], [12, 3, 6], [18, 14]]

result = []
for s in a:
    s = set(s)
    for t in result:
        if t & s:
            t.update(s)
            break
    else:
        result.append(s)

这将在列表中逐个进行,并从当前子列表(s)创建一个集合。然后它会检查结果,如果有另一个集合t与它有非空交集。如果是这种情况,则s中的项目会添加到该集t。如果没有t非空交集,则s是一个新的独立结果,可以附加到结果列表中。

像这样的问题也是fixed-point iteration的一个很好的例子。在这种情况下,只要您仍然可以找到重叠的列表,您将查看列表并继续合并子列表。您可以使用itertools.combinations来实现此目的,以查看成对的子列表:

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

答案 1 :(得分:1)

我能想到的一种方法是通过递归。从一个项目开始,然后循环,直到找到它所连接的每个数字。对于这些数字中的每一个,您必须执行相同的操作。因此递归。为了提高效率,请将您访问过的数字存储在列表中,并在每个递归序列的开头检查它,以确保不重复任何探索。

答案 2 :(得分:0)

两个班轮:

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]

答案 3 :(得分:-1)

我离开了最后一步:

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

答案 4 :(得分:-1)

简单回答:

 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

相关问题