gpt4 book ai didi

python-3.x - 如何通过 Python 上的循环将笛卡尔积保存到数据框?

转载 作者:行者123 更新时间:2023-12-05 04:39:00 25 4
gpt4 key购买 nike

我有以下字典:

the_dictionary_list = {'Fondo': ['Oceano.png'],
'Cuerpo': ['Cuerpo_cangrejo.png'],
'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'],
'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'],
'Pinzas': ['None', 'Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'],
'Puas': ['None', 'Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}

为了获得每个可能的排列而不按特定顺序重复(即笛卡尔积),我使用以下代码:

import itertools as it


AllKeysNames = ['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas', 'Puas']
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames)))
print(f'{Combinations}')

我怎样才能让上面的程序将每次迭代保存到一个数据帧中,例如它会抛出这样的输出:

Index   |                    Permutations   |        FilePermutations
0 |Fondo+Cuerpo+Ojos+Color+Pinzas+Puas|Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+None
1 |Fondo+Cuerpo+Ojos+Color+Pinzas+Puas|Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Arena.png
2 |Fondo+Cuerpo+Ojos+Color+Pinzas+Puas|Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Marron.png
.
.
.

Permutations 列中的信息将保持不变,数据帧的索引长度为 359

最佳答案

it.product 添加 join,然后为 Permutations 列添加 join:

AllKeysNames = ['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas', 'Puas']

new = ['+'.join(x) for x in
it.product(*(the_dictionary_list[Name] for Name in AllKeysNames))]

df = pd.DataFrame({'Permutations':"+".join(AllKeysNames), 'FilePermutations':new})

print (df)
Permutations \
0 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas
1 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas
2 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas
3 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas
4 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas
.. ...
355 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas
356 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas
357 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas
358 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas
359 Fondo+Cuerpo+Ojos+Color+Pinzas+Puas

FilePermutations
0 Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...
1 Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...
2 Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...
3 Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...
4 Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Ama...
.. ...
355 Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...
356 Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...
357 Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...
358 Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...
359 Oceano.png+Cuerpo_cangrejo.png+Verticales.png+...

[360 rows x 2 columns]

关于python-3.x - 如何通过 Python 上的循环将笛卡尔积保存到数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70492972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com