[Python] Python dictionary (Dictionary) update () method

update () function to the dictionary dict2 key / value pairs to update in dict. If duplicate keys behind overwrite the previous
grammar
dict.update (dict2)

dict = {'Name': 'Zara', 'Age': 7}
dict2 = {'Sex': 'female','Name':'zhangsan'}
dict.update(dict2)
print "Value : %s" % dict

result:

root@tao:/home/tao# python
Python 2.7.17 (default, Nov  7 2019, 10:07:09) 
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dict = {'Name': 'Zara', 'Age': 7}
>>> dict2 = {'Sex': 'female','Name':'zhangsan'}
>>> dict.update(dict2)
>>> print "Value : %s" %  dict
Value : {'Age': 7, 'Name': 'zhangsan', 'Sex': 'female'}
>>> 

 

The syntax is similar to php array_merge

The array_merge () or a plurality of the cell array combined, an array of values attached behind the front of an array. Returns an array as a result.
If the string has the same keys in the input array, the name of the key value of the previous value back cover. However, if the array contains numeric keys, the value of the latter will not overwrite the original value, but is appended to.
If only one array and the array is numerically indexed, the keys will be re-indexed in a continuous manner.

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Array
The above example will output:

(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

 

Guess you like

Origin www.cnblogs.com/taoshihan/p/12306713.html