簡體   English   中英

2 個列表的所有可能排列

[英]All possible permutations of 2 lists

如果我有 2 個列表:

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))]

我想得到 output 所有可能的排列(是 16^3 = 4096?),格式如下:

在此處輸入圖像描述

其中 [?,?,?] 表示所有排列值中的一組可能選項。

如何在 python 中實現這一點?

首先,我們將創建 256 個["val1", "val2", "val3", "val4"]的置換cases ,如4x4x4x4 = 256並且我們創建每個索引值的permutations列表,我們將在cases shuffling后添加

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)

輸出: 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM