1

I have this list:

[('airplane', 'ship'),
 ('car', 'truck'),
 ('bird', 'dog'),
 ('cat', 'horse'),
 ('cat', 'monkey'),
 ('dog', 'cat'),
 ('dog', 'deer'),
 ('horse', 'dog'),
 ('horse', 'monkey'),
 ('deer', 'cat'),
 ('deer', 'horse'),
 ('monkey', 'bird'),
 ('monkey', 'dog'),
 ('monkey', 'deer')]

I want to group these words as following:

1- Each pair words have the same group like; ('airplane', 'ship') -> airplane and ship in the same group ('car', 'truck'),-> 'car' and 'truck'in the same group

2-In the 2 pairs ('cat', 'horse'), ('cat', 'monkey'), i want to put "cat" and 'horse'and 'monkey' in the same group

3-Also In the 3 pairs ('monkey', 'bird'),('monkey', 'dog'), ('monkey', 'deer') i want to put 'monkey' and 'bird' and 'dog'and 'deer'in the same group

in the output i want to find this results:

[['airplane', 'ship'],['car', 'truck'],[ 'monkey' ,'bird','dog','deer','cat','horse']]

  • 2
    could you show your code to better understand what you mean and others can help? – Dini Jun 29 '22 at 12:18
  • 1
    What do you mean GROUP, like in a list ? for your example 3 output will be ['monkey', 'bird', 'dog', 'deer'], example 2 ['cat', 'horse', 'monkey'] & example 1 [airplane, ship] & ['car' & 'truck']. ? – Abhishek Jun 29 '22 at 12:23
  • groups like this : [['airplane', 'ship'],['car', 'truck'],[ 'monkey' ,'bird' ,'dog','deer',cat,horse,]] – Abdelmadjid Youcefa Jun 29 '22 at 12:57

1 Answers1

0

If the answer you are looking for is in the form proposed by Abhishek, I think the answer has already been given here:

Python: simple list merging based on intersections