0

Suppose my input is a list of lists:

[[1, 2], [3, 6], [1, 4], [3, 8]]

I need to get matches in the output, but not all in one list. For example, 1 lies in the list [1, 2] and also in the list [1, 4]. I want to get [1, 2, 4] and the same for 3.

That is, the result should be something like:

(1, 2, 4)
(3, 6, 8)

Is it possible?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Would a dictionary work better? e.g {1:[2,4],3:[6,8]} ? The number you are looking for always the first on in the lists in your example. what should this function return for f(2)? – JeffUK Jul 27 '21 at 19:18
  • You only want to match two elements of a list at a time and they can't be merged with other elements, right? like 0th n 1th index , then 2nd n 3rd index – Lakshika Parihar Jul 27 '21 at 19:19
  • "Is it possible?" If you can describe the criteria clearly, you should be able to write a program that implements it. So of course it's possible. – Barmar Jul 27 '21 at 19:23
  • they can't be merged with other elements - that's correct. – Dark Kitsune Jul 27 '21 at 19:25
  • In each list there is 2 elements always. but each element can be repeated in different lists unlimited times – Dark Kitsune Jul 27 '21 at 19:26
  • Do you control the function that is building the original list of lists? Can you change that function to write to a dictionary each time rather than (or as well as) to a list of lists? – JeffUK Jul 27 '21 at 19:28
  • @DarkKitsune you only want to merge two consecutive elements, right? What will be the desired output of this [[1,2],[1,4],[1,2],[1,2]]? – Lakshika Parihar Jul 27 '21 at 19:28
  • @JeffUK yes, i can use dictionary i think. now i try: list_of_lists = [[1,2], [1,4], [3,8], [9,3]] a = [set(l) for l in list_of_lists] b = a[0] c = a[0] for s in a[1:]: b = b.union(s) c = c.symmetric_difference(s) result = b.difference(c) print (result); but this way i get repetitions in one line. I need not to mix them.( – Dark Kitsune Jul 27 '21 at 19:36
  • @LakshikaParihar for [[1,2], [1,4], [1,2], [1,2]] it should be [1,2,4] – Dark Kitsune Jul 27 '21 at 19:38
  • Does this answer your question? [Merge lists that share common elements](https://stackoverflow.com/questions/4842613/merge-lists-that-share-common-elements) – Tomerikoo Jul 27 '21 at 19:46

3 Answers3

0
samples = [[1,2], [1,4], [3,6], [3,8]]
key = 3
list(set([l for j in [i for i in samples if key in i] for l in j]))
# [8, 3, 6]

The [i for i in samples if key in i] creates the list of candidates. The rest is the flattening of the list and with list(set(...)) you achieve that the sample are unique.

samusa
  • 449
  • 2
  • 11
0

The solution using the list_of_list

lists =  [[1, 2], [3, 6], [1, 4], [3, 8]]
indx = [0]*len(lists)
final=[]
for i in lists:
    temp =[]
    for k in range(len(lists)):
        if i[0] in lists[k] and indx[k]==0:
            indx[k] = 1
            temp+=lists[k]
    if len(temp)>0:
        final.append(set(temp))
print(final)

The output of the above code will be [{1, 2, 4}, {8, 3, 6}]

  • I created a list that keeps track of the elements already used
  • then I am checking the 0th index of every element of the list if it's not already checked
Lakshika Parihar
  • 1,017
  • 9
  • 19
0

just with list comprehension return a list of lists containing any of items you are looking for. then join (i found the sum method here) these lists and make a set

def get_matches(matches, alist):
  return set(sum([y for x in matches for y in alist if(x in y)], []))

get_matches([1,4], [[1,2], [1,4], [3,6], [3,8]])
get_matches([3], [[1,2], [1,4], [3,6], [3,8]])
malykony
  • 1
  • 4