[python]Python 字典(Dictionary) update()方法
update() 函数把字典dict2的键/值对更新到dict里。如果后面的键有重复的会覆盖前面的
语法
dict.update(dict2)
dict = {'Name': 'Zara', 'Age': 7}
dict2 = {'Sex': 'female','Name':'zhangsan'}
dict.update(dict2)
print "Value : %s" % dict
结果:
root@tao:/home/tao# python
Python 2.7. (default, Nov , ::)
[GCC 9.2. ] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dict = {'Name': 'Zara', 'Age': }
>>> dict2 = {'Sex': 'female','Name':'zhangsan'}
>>> dict.update(dict2)
>>> print "Value : %s" % dict
Value : {'Age': , 'Name': 'zhangsan', 'Sex': 'female'}
>>>
php中类似的语法是array_merge
array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一个值。然而,如果数组包含数字键名,后面的值将不会覆盖原来的值,而是附加到后面。
如果只给了一个数组并且该数组是数字索引的,则键名会以连续方式重新索引。
<?php
$array1 = array("color" => "red", , );
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", );
$result = array_merge($array1, $array2);
print_r($result);
?>
以上例程会输出: Array
(
[color] => green
[] =>
[] =>
[] => a
[] => b
[shape] => trapezoid
[] =>
)
[python]Python 字典(Dictionary) update()方法的更多相关文章
- Python字典(Dictionary)update()方法
原文连接:https://www.runoob.com/python/att-dictionary-update.html Python字典(dictionary)update()函数把字典dict2 ...
- Python 字典(Dictionary) update()方法
refer to: http://www.runoob.com/python/att-dictionary-update.html
- Python 字典(Dictionary) clear()方法
Python 字典(Dictionary) clear()方法 描述 Python 字典(Dictionary) clear() 函数用于删除字典内所有元素.高佣联盟 www.cgewang.com ...
- Python 字典(Dictionary) type()方法
Python 字典(Dictionary) type()方法 描述 Python 字典(Dictionary) type() 函数返回输入的变量类型,如果变量是字典就返回字典类型.高佣联盟 www.c ...
- Python 字典(Dictionary) str()方法
Python 字典(Dictionary) str()方法 描述 Python 字典(Dictionary) str() 函数将值转化为适于人阅读的形式,以可打印的字符串表示.高佣联盟 www.cge ...
- Python 字典(Dictionary) len()方法
Python 字典(Dictionary) len()方法 描述 Python 字典(Dictionary) len() 函数计算字典元素个数,即键的总数.高佣联盟 www.cgewang.com 语 ...
- Python 字典(Dictionary) cmp()方法
Python 字典(Dictionary) cmp()方法 描述 Python 字典的 cmp() 函数用于比较两个字典元素.高佣联盟 www.cgewang.com 语法 cmp()方法语法: cm ...
- Python 字典(Dictionary) get()方法
描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
- Python 字典(Dictionary) setdefault()方法
描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...
随机推荐
- 原型链污染(Node.js污染,javasrcipt原型链污染的)
学习链接: https://www.jianshu.com/p/6e623e9debe3 关于NJS https://xz.aliyun.com/t/7184 相关题是 GYCTF ez_expr ...
- 虚拟机floppy0
网上搜索方法是:删除该虚拟机的软盘即可. 具体原因还不知道,以后再补上原因
- rabbitmq系列问题解决:406, "PRECONDITION_FAILED - inequivalent arg 'durable'
1. 安装rabbitmq,查看官网文档: https://www.rabbitmq.com/#getstarted 由于我是先安装了rabbitmq后自己随手创建了queue,后面又按照官方给的&q ...
- LED Keychain-Ideal For Mass Promotions
Looking for something memorable to remind people of your business or nonprofit? Consider custom LED ...
- spring boot no identifier specified for entity
定义Id 时,引用的是 import org.springframework.data.annotation.Id; 实际应该引入: import javax.persistence.Id;
- Linux C++ 直接选择排序,冒泡排序,快速排序
选择排序的思想是:每次从待排序中选择最小(大)的元素插入已经排好的序列中. /*直接选择排序*/ #include <iostream> using namespace std; void ...
- leetcode腾讯精选练习之相交链表(六)
相交链表 题目: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5 ...
- git上传代码技巧
1.一定要先在git上面创建项目然后把文件拉到本地先 1.1克隆到本地(orgin=>'你的远程仓库地址') git clone orgin 1.2初始化项目文件夹 git init 2.操作之 ...
- kao shi di er ti(还没有订正)
// 离散化点 思路应该是对的 吧 但没时间去检查编译上的错误 #include <bits/stdc++.h> using namespace std; ; #define ri reg ...
- K-NN graph
tasks: 1. unsupervised knn https://scikit-learn.org/stable/modules/neighbors.html#unsupervised-neigh ...