简体   繁体   中英

All possible permutations of 2 lists

If I have 2 lists:

keys= ["key1", "key2", "key3", "key4", "key5", "key6", "key7", "key8",
         "key9", "key10", "key11", "key12", "key13", "key14", "key15", "key16"]

values= ["val1", "val2", "val3"]

# permutations = [list(zip(x, values)) for x in itertools.permutations(keys, len(values))]

I want to get as output all possible permutations (is it 16^3 = 4096?) in such format:

在此处输入图像描述

where [?,?,?] represents a set of possible options within all permutations values.

How can I achieve that in python?

First we are going to create 256 permutation cases of ["val1", "val2", "val3", "val4"] as 4x4x4x4 = 256 and we create permutations list whose each index value, we will add after shuffling of cases

import itertools
import pandas as pd
import random
import numpy as np

coln = [f'Col{i}' for i in range(1, 257)]
values= ["val1", "val2", "val3", "val4"]
cases = [p for p in itertools.product(values, repeat= 4)]
keys= ["key1", "key2", "key3", "key4", "key5", "key6", "key7", "key8",
         "key9", "key10", "key11", "key12", "key13", "key14", "key15", "key16"]

permutations = []
for _ in range(16):
    random.shuffle(cases)
    permutations.append(cases)

df = pd.DataFrame(data = permutations, index= keys, columns= coln, dtype= np.object)
print(df)

OUPTUT: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM