-2

For starters, lets consider two different float values a and b and an integer d. I would like to create a numpy.array that contain all permutations of

(b, a, a, ..., a)

where the length of the vector is d+1. For example, d=2, I would like to get

a = 3.14
b = 2.71
numpy.array([
    [b, a, a],
    [a, b, a],
    [a, a, b],
    ])

While this case could be quite easily generated by hand, more interesting to me are permutations of

(b, b, a, a, ..., a)
(b, c, a, a, ..., a)
(b, b, b, a, ..., a)

(where c is a float different from a and b).

The first one would start off as

numpy.array([
    [b, b, a, ..., a],
    [b, a, b, ..., a],
    ...
    [b, a, ..., a, b],
    [a, b, b, a, ..., a],
    ...
    ])

Any hints?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • 3
    I don't get the logic. Do you have a concrete example, with numbers instead of variable names? Also, what did you try? – Eric Duminil Jul 15 '17 at 11:15
  • 1
    Why is it filled with `a` in the first example but with `b` and `a` in the second? Is there any "requirement" missing from the problem description? – MSeifert Jul 15 '17 at 11:16
  • 1
    The second one (with the `c` would need some explanation -- the first one is quite simple -- just create an array full of `a` and put `b` on the diagonal. – Thomas Kühn Jul 15 '17 at 11:16
  • @EricDuminil I don't understand what's unclear. Would you like me to fill in `a=3.123123` and `b=5.1213`? – Nico Schlömer Jul 15 '17 at 11:19
  • 1
    Possible duplicate of [permutations with unique values](https://stackoverflow.com/questions/6284396/permutations-with-unique-values) – Eric Duminil Jul 15 '17 at 11:31
  • 2
    This question has already been answered here, with an efficient algorithm : https://stackoverflow.com/a/6285203/6419007 `set(itertools.permutations)` works in theory but is highly inefficient. – Eric Duminil Jul 15 '17 at 11:32

1 Answers1

3

You can use itertools.permutations for this task.

from itertools import permutations

a = [1.0, 2.0, 2.0]
perms = set(permutations(a))
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
Vibhutha Kumarage
  • 1,372
  • 13
  • 26