Thread: PHP to Python

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    207
    Rep Power
    16

    PHP to Python


    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,
    Code:
    __________________
    
    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
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,988
    Rep Power
    510
    The php program computes the Cartesian product. In j j programming language the monadic { verb computes Cartesian product
    Code:
       _2<\'ABCDEF'
    ┌──┬──┬──┐
    │AB│CD│EF│
    └──┴──┴──┘
    
       {_2<\'ABCDEF'
    ┌───┬───┐
    │ACE│ACF│
    ├───┼───┤
    │ADE│ADF│
    └───┴───┘
    
    ┌───┬───┐
    │BCE│BCF│
    ├───┼───┤
    │BDE│BDF│
    └───┴───┘
    In python, well, I don't understand the php program and, while I could write an explicit Cartesian product function, it wouldn't be a translation and would not help. If you want to become a python programmer, then work through the tutorial https://docs.python.org/3/tutorial/index.html and learn the standard library https://docs.python.org/3/library/index.html
    Code:
    import itertools  # itertools.product implements the Cartesian product
    
    # expressed with python syntactic sugar
    
    result = [cp for cp in itertools.product(*'AB CD EF'.split())]
    
    print(result)
    
    
    
    # equivalent expression written in a long form
    
    result_longhand = list()
    
    for cp in itertools.product('AB', 'CD', 'EF'):
        result_longhand.append(cp)
    
    print('agreement? ', result_longhand == result)
    
    
    
    data = 'AB CD EF'.split()  # I find this easier to type than ['AB', 'CD', 'EF']
    
    # itertools.product(*array_of_arguments) is shorthand and generalization of
    # itertools.product(data[0], data[1], data[2])
    
    
    
    
    '''
        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);
    
    output:
    
    array(8) {
      [0]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "C" ["item3"]=> string(1) "E" }
      [1]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "C" ["item3"]=> string(1) "F" }
      [2]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "D" ["item3"]=> string(1) "E" }
      [3]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "D" ["item3"]=> string(1) "F" }
      [4]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "C" ["item3"]=> string(1) "E" }
      [5]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "C" ["item3"]=> string(1) "F" }
      [6]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "D" ["item3"]=> string(1) "E" }
      [7]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "D" ["item3"]=> string(1) "F" }
    }
    
    '''
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    207
    Rep Power
    16
    Thank you b49P23TIvg,

    Your code is great! and very helpful! I will study the tutorials and the library. I had to work on this complex thing being a beginner in Python and I had to ask. Thanks again!!

    Best,
    Mapg
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2019
    Posts
    1
    Rep Power
    0
    Thanks, very informative for me too. I'm a beginner in Python like a topic starter

IMN logo majestic logo threadwatch logo seochat tools logo