Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP to Python
#9
(Apr-27-2019, 02:41 PM)snippsat Wrote: For Python could use itertools combinations().
import itertools as it
from pprint import pprint

my_dict = {'item1':['A', 'B'], 'item':['C', 'D'], 'item3':['E','F']}
combinations = it.product(*(my_dict[name] for name in my_dict))
all_combo = list(combinations)
print(f'size={len(all_combo)}')
pprint(all_combo)
Output:
size=8 [('A', 'C', 'E'), ('A', 'C', 'F'), ('A', 'D', 'E'), ('A', 'D', 'F'), ('B', 'C', 'E'), ('B', 'C', 'F'), ('B', 'D', 'E'), ('B', 'D', 'F')]

The answer is very good but unfortunately, the keys are lost, and I need them.

How can be preserved the output that PHP version is showing but using Python?, namely ...
Output:
array (size=8) 0 => array (size=3) 'item1' => string 'A' (length=1) 'item2' => string 'C' (length=1) 'item3' => string 'E' (length=1) 1 => array (size=3) 'item1' => string 'A' (length=1) 'item2' => string 'C' (length=1) 'item3' => string 'F' (length=1) 2 => etc ...
Reply


Messages In This Thread
PHP to Python - by mapg - Apr-27-2019, 04:31 AM
RE: PHP to Python - by Gribouillis - Apr-27-2019, 04:57 AM
RE: PHP to Python - by mapg - Apr-27-2019, 01:16 PM
RE: PHP to Python - by snippsat - Apr-27-2019, 02:41 PM
RE: PHP to Python - by mapg - Apr-28-2019, 01:14 AM
RE: PHP to Python - by mapg - Apr-27-2019, 02:53 PM
RE: PHP to Python - by buran - Apr-27-2019, 03:37 PM
RE: PHP to Python - by mapg - Apr-27-2019, 07:19 PM
RE: PHP to Python - by Gribouillis - Apr-27-2019, 08:45 PM
RE: PHP to Python - by rxndy - Apr-28-2019, 01:21 AM
RE: PHP to Python - by buran - Apr-28-2019, 06:58 AM
RE: PHP to Python - by mapg - Apr-28-2019, 03:52 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020