Ok, while I maybe wrong, I have the feeling I can solve this without the help of a function. With this kind of input list:
input=[[1,2,3],[4,5],[7],[5,4,6],[5,4]]
I want a resulting list where inner lists sharing a common element are concatenated:
result=[[1,2,3],[4,5,6],[7]]
I already found the
if any(x in result[j] for x in input[i])
syntax that seems helpful for my problem, but now I'm stuck.
[EDIT] one of my trials:
input=[[1,2,3],[4,5],[7],[5,4,6],[5,4]]
result=[input[0]]
for i in range(len(input)):
for j in range(len(result)):
if result[j] != input[i]:
if any(x in result[j] for x in input[i]):
result[j].extend(input[i])
result[j]=list(set(result[j]))
print "concatenate ",result[j], input[i]
break
else :
result.append(input[i])
print "append ", result[j], input[i]
break
>>> (bad) result
[[1, 2, 3], [4, 5], [7], [5, 4, 6], [5, 4]]
While if I initialize result with result=[input[-1]] it works:
input=[[1,2,3],[4,5],[7],[5,4,6],[5,4]]
result=[input[-1]]
for i in range(len(input)):
for j in range(len(result)):
if result[j] != input[i]:
if any(x in result[j] for x in input[i]):
result[j].extend(input[i])
result[j]=list(set(result[j]))
print "concatenate ",result[j], input[i]
break
else :
result.append(input[i])
print "append ", result[j], input[i]
break
>>> (good) result
[[4, 5, 6], [1, 2, 3], [7]]