Array in php and dict in python same?

I have a project using python and I want to convert php to python. I got confused in php array when converting it to python ...

in old php code ... it looks like this:

array( "Code" => 122, "Reference" => 1311, "Type" => 'NT', "Amount" => 100.00 ); 

and this is what I did to convert it to python ...

 dict = { "Code":122, "Reference":1311, "Type":'NT', "Amount":100.00 } 

My php conversion to python is correct?

+6
source share
3 answers

Your conversion is essentially correct (although I would not use dict as a variable name, since it masks the built-in class constructor with the same name). At the same time, PHP arrays are ordered mappings, so you should use Python OrderedDict instead of the usual dict in order to preserve the insertion order:

 >>> import collections >>> od = collections.OrderedDict([ ('Code', 122), ('Reference', 1311), ('Type', 'NT'), ('Amount', 100.00), ]) >>> print od['Amount'] 100.0 >>> od.keys() ['Code', 'Reference', 'Type', 'Amount'] 
+12
source

To be specific, the PHP associative array is the same as the Python dictionary, and the PHP associative array is ordered. Ruby hash tables are the same as PHP associative arrays (ordered key-value pairs).

Python lists and PHP arrays are the same (unordered lists of unqualified values). Ruby arrays are the same as Python and PHP.

0
source

PHP arrays are different from py objects, which are:

  • PHP keys can be integer or string (associative),
  • When a value is added to the array, a digital key is automatically assigned before the key / value pair is added to the array. The assigned associated key has an integer value equal to the maximum value from the internal numeric iterator pointers of the array plus 1,
  • Numeric integer keys in a string are displayed as integers,
  • The insertion order into the array is preserved. key / value pairs are repeated in accordance with the key entry order. We can use Python's Orderedict to follow,
  • PHP array[] = 5 translated into Python array.append(5) or array[None] = 5 ,
  • The keys of the iterator indexer are processed through the built-in PHP functions or passed by reference, with a copy to the write policy.

I would like to provide a plugin for my newly released pyx.php Python module. There is a Python array class in the module that emulates a PHP array. Our Python emulation of a PHP array uses an instance variable OrderedDict, where array._obj is OrderedDic to store all elements of the array and track their insert orders using a special instance of the pointer variable. Try:

 $ git clone https://github.com/wordpy/pyx/ $ python # or ipython` >>> import pyx.php as Php; array = Php.array >>> arr1 = array( (0,'1-0'),('a','1-a'),('b','1-b'),) >>> arr2 = array( (0,'2-0'),( 1,'2-1'),('b','2-b'),('c','2-c'),) >>> arr1 + arr2 # same as: Php.array_plus(arr1, arr2), see below >>> Php.array_merge(arr1, arr2) >>> import pyx.php as Php; array = Php.array >>> Arr0 = array() # Arr0._obj is an empty OrderedDict() >>> Arr1 = array( ('a',11), 'zzz', (99,99), 22, 33, (2,22) ) >>> Arr1 array(6) { ['a']=> <int> 11 [0]=> <str> zzz [99]=> <int> 99 [100]=> <int> 22 [101]=> <int> 33 [2]=> <int> 22 } 

zip() works for an array with different len ​​!!!

 >>> for i,j in zip( array(1,2,3,4), array(11,22,33) ): ... print(i,j) 1 11 2 22 3 33 >>> for i,j in zip( array(1,2), array(11,22,33) ): ... print(i,j) 1 11 2 22 

array() in the pyx.php module Cython offers almost everything that PHP offers array (), as well as many other methods. See https://wordpy.com/pyx/php/ .

pyx.php is currently only available for Python 3.x, which runs on a 64-bit version of Linux. Python 2.x, Mac, or other platforms can be compiled when there are many requests.

0
source

Source: https://habr.com/ru/post/906721/


All Articles