0

I have a 6 group of number:

data = np.array([['清新','妩媚'],['英气','温婉'], ['俏皮', '风雅'], 
            ['简洁', '华丽'], ['端庄','风情'], ['清凉','保暖']])

# combination
i = 0
sets = []
while i < 6:
    j = i + 1
    while j < 6:
        g = j + 1
        while g < 6:
            sets.append([i,j,g])
            g += 1
        j+=1
    i+=1
sets

loop_val_all = [data[i] for i in sets] 

fn = lambda x, code=',': reduce(lambda x, y: [str(i) + str(j) for i in x for j in y], x)

combinations = []
for loop_val in loop_val_all:
    combinations.append(fn(loop_val))

Each time, I will pick one number from three groups like '清新英气俏皮'. The order is not considered.

So any other idea about how can I return all the combinations using python?

I got 160 sets. I think the result is fine, but my calculation is kind of complex.

['清新英气俏皮',
 '清新英气风雅',
 '清新温婉俏皮',
 '清新温婉风雅',
 '妩媚英气俏皮',
 '妩媚英气风雅', ...]
Wei Zhang
  • 47
  • 4
  • What have you tried so far? – RMPR Apr 16 '20 at 09:44
  • I don't understand how you're choosing these numbers. Why 6 * 4 * 3 * 2? – Alex Hall Apr 16 '20 at 09:46
  • C(n = 6, m = 3) * 2 – Wei Zhang Apr 16 '20 at 10:08
  • [6 choose 3 is 20](https://www.google.com/search?q=6+choose+3), and since there are three pairs from which to choose a number, you should multiply by 2^3=8 – Alex Hall Apr 16 '20 at 10:33
  • Use random.sample and random.choice https://docs.python.org/3/library/random.html#random.sample – Alex Hall Apr 16 '20 at 10:37
  • You should consider replacig the chinese letters with latin letters or something similar. At the moment it is not really clear what want to achive. It would be great if you could provide a minimal working example (mwe). Explain what you input is and how your desired output looks like. – MachineLearner Apr 16 '20 at 10:42
  • Does this answer your question? [Python Itertools permutations with strings](https://stackoverflow.com/questions/44449311/python-itertools-permutations-with-strings) – Joe Apr 16 '20 at 11:21
  • https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list – Joe Apr 16 '20 at 11:21

2 Answers2

1
import itertools


def combos(list_of_words):
    for pair_combination in itertools.combinations(list_of_words, 3):
        yield from itertools.product(*pair_combination)


for t in combos([
    ['a', 'b'],
    ['c', 'd'],
    ['e', 'f'],
    ['g', 'h'],
    ['i', 'j'],
    ['k', 'l'],
]):
    print("".join(t))
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
0

Not sure if this is what you are looking for

import itertools

list_of_words = [
  ['清新','妩媚'],
  ['英气','温婉'],
  ['俏皮', '风雅'],
  ['简洁', '华丽'],
  ['端庄','风情'],
  ['清凉','保暖']]

permutations = list(itertools.permutations(list_of_words))

print(permutations)
MachineLearner
  • 805
  • 1
  • 8
  • 22