-2

I try do enumerate all the group of 2 (possible) in a list of people. Like for example for a group project I went to have all the possibilities of group of two people in the list of the class. (In python).

For example if my list of people is: {a,b,c,d,e,f} I want to have:

enter image description here

I tried a lot of things (itertools.combinations() or itertools.permutations()) but I don't succeed to have this result without tuples or without two times the same person in a group.

ZF007
  • 3,708
  • 8
  • 29
  • 48
  • Possible duplicate of [How to split a list into pairs in all possible ways](https://stackoverflow.com/questions/5360220/how-to-split-a-list-into-pairs-in-all-possible-ways) – Georgy Nov 18 '19 at 10:16

3 Answers3

1

You can do the following (copied from https://stackoverflow.com/a/5360442):

lst =  ['a','b','c','d','e','f']

def all_pairs(lst):
    if len(lst) < 2:
        yield []
        return
    if len(lst) % 2 == 1:
        # Handle odd length list
        for i in range(len(lst)):
            for result in all_pairs(lst[:i] + lst[i+1:]):
                yield result
    else:
        a = lst[0]
        for i in range(1,len(lst)):
            pair = [a,lst[i]]
            for rest in all_pairs(lst[1:i]+lst[i+1:]):
                yield [pair] + rest

print(list(all_pairs(lst)))

which gives you:

[[['a', 'b'], ['c', 'd'], ['e', 'f']],
 [['a', 'b'], ['c', 'e'], ['d', 'f']],
 [['a', 'b'], ['c', 'f'], ['d', 'e']],
 [['a', 'c'], ['b', 'd'], ['e', 'f']],
 [['a', 'c'], ['b', 'e'], ['d', 'f']],
 [['a', 'c'], ['b', 'f'], ['d', 'e']],
 [['a', 'd'], ['b', 'c'], ['e', 'f']],
 [['a', 'd'], ['b', 'e'], ['c', 'f']],
 [['a', 'd'], ['b', 'f'], ['c', 'e']],
 [['a', 'e'], ['b', 'c'], ['d', 'f']],
 [['a', 'e'], ['b', 'd'], ['c', 'f']],
 [['a', 'e'], ['b', 'f'], ['c', 'd']],
 [['a', 'f'], ['b', 'c'], ['d', 'e']],
 [['a', 'f'], ['b', 'd'], ['c', 'e']],
 [['a', 'f'], ['b', 'e'], ['c', 'd']]]

As required.

Karl Anka
  • 2,529
  • 1
  • 19
  • 30
CDJB
  • 14,043
  • 5
  • 29
  • 55
  • Now i have to handle the possibilite of a odd number of people. So with this possibilitie now i can have not only group of two people but also three. We try to keep this algorithm and put the extra personne on the groupes of two but this don’t provide all the possibilities because the extra person is never in a group of two... Do you have an idea of how we can handle this problem ? – Justine FOULQUIER Nov 19 '19 at 17:06
0

You can use this built-in function

import itertools
data = ['a', 'b', 'c', 'd', 'e', 'f']
#in case the number of items is odd
len(data) % 2 != 0 and data.append(None)
number_of_groups = int(len(data) / 2)
check = lambda x, y: not list(set(x) & set(y))
test  = lambda grps : all([check(x[0], x[1]) for x in itertools.combinations(grps, 2)])
pairs = [list(x) for x in itertools.combinations(['a', 'b', 'c', 'd', 'e', 'f'], 2)]
[list(x) for x in itertools.combinations(pairs, number_of_groups) if test(x)]

The result is [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['a', 'b'], ['c', 'e'], ['d', 'f']], [['a', 'b'], ['c', 'f'], ['d', 'e']], [['a', 'c'], ['b', 'd'], ['e', 'f']], [['a', 'c'], ['b', 'e'], ['d', 'f']], [['a', 'c'], ['b', 'f'], ['d', 'e']], [['a', 'd'], ['b', 'c'], ['e', 'f']], [['a', 'd'], ['b', 'e'], ['c', 'f']], [['a', 'd'], ['b', 'f'], ['c', 'e']], [['a', 'e'], ['b', 'c'], ['d', 'f']], [['a', 'e'], ['b', 'd'], ['c', 'f']], [['a', 'e'], ['b', 'f'], ['c', 'd']], [['a', 'f'], ['b', 'c'], ['d', 'e']], [['a', 'f'], ['b', 'd'], ['c', 'e']], [['a', 'f'], ['b', 'e'], ['c', 'd']]]

Co Worker
  • 139
  • 2
  • 10
0
import itertools

people = ['a', 'b', 'c', 'd', 'e', 'f']
# number of groups that could be created
n_groups = len(people) // 2

# create all possible pairs
pairs = itertools.combinations(people, 2)

# create all group constellations
group_combo = itertools.combinations(pairs, n_groups)

# check for impossible constellations
# ie. it is not possible to have 'a' in two groups
for group in group_combo:
    flatten_group_tuple = [element for tupl in group for element in tupl]
    # check for duplicate members, if duplicates exist the set-size will be < n_groups * 2
    if len(set(flatten_group_tuple)) == n_groups * 2:
        print([list(x) for x in group])
Karl Anka
  • 2,529
  • 1
  • 19
  • 30