Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP to Python
#1
Hi All,

I learning Python and I see that my knowledge of this language is very poor as I am trying to translate a function from PHP to Python but I cannot get results.

This is the code in PHP and below, my try in Python,
__________________

PHP Code
__________________

function getCombinations($arrays) {
	$result = array(array());
	foreach ($arrays as $property => $property_values) {
		$tmp = array();
		foreach ($result as $result_item) {
			foreach ($property_values as $property_value) {
				$result_item[$property] = $property_value;
				$tmp[] = $result_item;
			}
		}
		$result = $tmp;
	}
	return $result;
}

$combinations = getCombinations(
	array(
		'item1' => array('A', 'B'),
		'item2' => array('C', 'D'),
		'item3' => array('E', 'F'),
	)
);

var_dump($combinations);
__________________

MY TRY IN PYTHON
__________________

def getCombinations(arrays):
	result = []
	for property, property_values in arrays.items():
		tmp = []
		for result_item in result:
			for property_value in property_values:
				result_item[property] = property_value
				tmp.append(result_item)
		result = tmp
	return result
My intention is to learn where are my faults to continue learning.

Thanks a lot in advance.

Mapg
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