[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()方法语法: ...
随机推荐
- nodeJS菜鸟教程笔记
http模块 var http = require('http'); // 引入http模块 var url = require('url'); // 引入url模块 var querystring ...
- java - 锁的种类及详解
锁类型 锁根据其特性能够划分出各种各样的锁类型,该文主要介绍以下锁的作用及特性 乐观锁/悲观锁 独享锁/共享锁 互斥锁/读写锁 可重入锁 公平锁/非公平锁 分段锁 偏向锁/轻量级锁/重量级锁 自旋锁 ...
- 点击按钮出现60秒倒计时,JS(按钮)
<script type="text/javascript"> var countdown=60; function settime(val) { var e = $( ...
- 一道CTF针对XXE漏洞的练习
题目链接:http://web.jarvisoj.com:9882/ 目的很明确获取/home/ctf/flag.txt的内容 一般读取目标机的本地文件都会用到file协议. 思路: 那么思路一:文件 ...
- 初识压缩感知Compressive Sensing
压缩感知是近年来极为热门的研究前沿,在若干应用领域中都引起瞩目.最近粗浅地看了这方面一些研究,对于Compressive Sensing有了初步理解,在此分享一些资料与精华.本文针对陶哲轩和Emman ...
- [ZJOI2008] 骑士 - 基环树dp
一类基环树dp都是这个套路吧 随便拆掉环上的一条边 然后跑树形dp,设\(f[i][0/1]\)表示以第\(i\)个人为根的子树,第\(i\)个人选或不选,能收获的最大值 以断点\(u,v\)为根分别 ...
- linux安装php-laravel环境
1.运用传说中的宝塔面板安装(https://www.bt.cn/download/linux.html)网站地址 在xshell软件中安装一下命令 1.1 宝塔centos安装 wget -O in ...
- 字符串匹配算法--KMP字符串搜索(Knuth–Morris–Pratt string-searching)C语言实现与讲解
一.前言 在计算机科学中,Knuth-Morris-Pratt字符串查找算法(简称为KMP算法)可在一个主文本字符串S内查找一个词W的出现位置.此算法通过运用对这个词在不匹配时本身就包含足够的信息 ...
- (转)git学习教程
转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
- Android 开发替换Launcher
做android产品的时候,根据需求会制定各种各样的Launcher,因此,在此记录替换系统Launcher的流程. 1.修改frameworks/base/core/java/android/con ...