【发布时间】:2012-11-22 18:40:58
【问题描述】:
我有一个列表,如我附加的代码中所示。如果有任何共同值,我想链接每个子列表。然后我想用一个精简的列表列表替换列表列表。 示例:如果我有一个列表[[1,2,3],[3,4]],我想要[1,2,3,4]。如果我有[[4,3],[1,2,3]],我想要[4,3,1,2]。如果我有[[1,2,3],[a,b],[3,4],[b,c]] 我想要[[1,2,3,4],[a,b,c]] 或[[a,b,c],[1,2,3,4]] 我不在乎是哪一个。
我快到了……
我的问题是当我遇到[[1,2,3],[10,5],[3,8,5]] 这样的案例时,我想要[1,2,3,10,5,8],但使用我当前的代码我得到[1,2,3,8,10,5]
这是我的代码:
import itertools
a = [1,2,3]
b = [3,4]
i = [21,22]
c = [88,7,8]
e = [5,4]
d = [3, 50]
f = [8,9]
g= [9,10]
h = [20,21]
lst = [a,b,c,i,e,d,f,g,h,a,c,i]*1000
#I have a lot of list but not very many different lists
def any_overlap(a, b):
sb = set(b)
return any(itertools.imap(sb.__contains__, a))
def find_uniq(lst):
''' return the uniq parts of lst'''
seen = set()
seen_add = seen.add
return [ x for x in lst if x not in seen and not seen_add(x)]
def overlap_inlist(o_lst, lstoflst):
'''
Search for overlap, using "any_overlap", of a list( o_lst) in a list of lists (lstoflst).
If there is overlap add the uniq part of the found list to the search list, and keep
track of where that list was found
'''
used_lst =[ ]
n_lst =[ ]
for lst_num, each_lst in enumerate(lstoflst):
if any_overlap(o_lst,each_lst):
n_lst.extend(each_lst)
used_lst.append(lst_num)
n_lst= find_uniq(n_lst)
return n_lst, used_lst
def comb_list(lst):
'''
For each list in a list of list find all the overlaps using 'ovelap_inlist'.
Update the list each time to delete the found lists. Return the final combined list.
'''
for updated_lst in lst:
n_lst, used_lst = overlap_inlist(updated_lst,lst)
lst[:] = [x for i,x in enumerate(lst) if i not in used_lst]
lst.insert(0,n_lst)
return lst
comb_lst = comb_list(lst)
print comb_lst
这个脚本的输出是:
[[88, 7, 8, 9, 10], [1, 2, 3, 4, 50, 5], [21, 22, 20]]
我想要它,所以密钥按原始顺序排列,例如:
[[88, 7, 8, 9, 10], [1, 2, 3, 4, 5, 50,], [21, 22, 20]]
5和50切换在新的lst[2]
我对 python 有点陌生。我将不胜感激对我当前代码的问题或 cmets 的任何解决方案。我不是计算机科学家,我想可能有某种算法可以快速做到这一点(也许来自集合论?)。如果有这样的算法,请指出我正确的方向。
我可能会让这种方式变得更复杂,然后...... 谢谢!!
【问题讨论】:
-
[10, 5]和[1,2,3,8]有哪些共同的价值观? -
我编辑了它。我在其中一个列表中添加了 5。关键是如果列表尚未按顺序排列,它会更改顺序。
-
@keithchem 你好。我花了很长时间才解决你的问题。请看我的方法,告诉我你的想法。
-
@keithchem 有趣且困难的问题。 +1
标签: python algorithm python-2.x nested-lists itertools