广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python Dict用法
  • 131
分享到

Python Dict用法

PythonDict 2023-01-31 07:01:11 131人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

#字典的添加、删除、修改操作dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}dict["w"] = "watermelon"del(dict["a"]

#字典的添加、删除、修改操作

dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}

dict["w"] = "watermelon"

del(dict["a"])

dict["g"] = "grapefruit"

print dict.pop("b")

print dict

dict.clear()

print dict

#字典的遍历

dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}

for k in dict:

    print "dict[%s] =" % k,dict[k]

#字典items()的使用

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}

#每个元素是一个key和value组成的元组,以列表的方式输出

print dict.items()

#调用items()实现字典的遍历

dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}

for (k, v) in dict.items():

    print "dict[%s] =" % k, v

#调用iteritems()实现字典的遍历

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}

print dict.iteritems()

for k, v in dict.iteritems():

    print "dict[%s] =" % k, v

for (k, v) in zip(dict.iterkeys(), dict.itervalues()):

    print "dict[%s] =" % k, v

   


#使用列表、字典作为字典的值

dict = {"a" : ("apple",), "bo" : {"b" : "banana", "o" : "orange"}, "g" : ["grape","grapefruit"]}

print dict["a"]

print dict["a"][0]

print dict["bo"]

print dict["bo"]["o"]

print dict["g"]

print dict["g"][1]

 

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}

#输出key的列表

print dict.keys()

#输出value的列表

print dict.values()

#每个元素是一个key和value组成的元组,以列表的方式输出

print dict.items()

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}

it = dict.iteritems()

print it

#字典中元素的获取方法

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}

print dict

print dict.get("c", "apple")         

print dict.get("e", "apple")

#get()的等价语句

D = {"key1" : "value1", "key2" : "value2"}

if "key1" in D:

    print D["key1"]

else:

    print "None"

#字典的更新

dict = {"a" : "apple", "b" : "banana"}

print dict

dict2 = {"c" : "grape", "d" : "orange"}

dict.update(dict2)

print dict

#udpate()的等价语句

D = {"key1" : "value1", "key2" : "value2"}

E = {"key3" : "value3", "key4" : "value4"}

for k in E:

    D[k] = E[k]

print D

#字典E中含有字典D中的key

D = {"key1" : "value1", "key2" : "value2"}

E = {"key2" : "value3", "key4" : "value4"}

for k in E:

    D[k] = E[k]

print D

#设置默认值

dict = {}

dict.setdefault("a")

print dict

dict["a"] = "apple"

dict.setdefault("a","default")

print dict

#调用sorted()排序

dict = {"a" : "apple", "b" : "grape", "c" : "orange", "d" : "banana"}

print dict  

#按照key排序 

print sorted(dict.items(), key=lambda d: d[0])

#按照value排序 

print sorted(dict.items(), key=lambda d: d[1])

#字典的浅拷贝

dict = {"a" : "apple", "b" : "grape"}

dict2 = {"c" : "orange", "d" : "banana"}

dict2 = dict.copy()

print dict2


#字典的深拷贝

import copy

dict = {"a" : "apple", "b" : {"g" : "grape","o" : "orange"}}

dict2 = copy.deepcopy(dict)

dict3 = copy.copy(dict)

dict2["b"]["g"] = "orange"

print dict

dict3["b"]["g"] = "orange"

print dict


--结束END--

本文标题: Python Dict用法

本文链接: https://www.lsjlt.com/news/190771.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python Dict用法
    #字典的添加、删除、修改操作dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}dict["w"] = "watermelon"del(dict["a"]...
    99+
    2023-01-31
    Python Dict
  • Python dict函数用法详解
    dict函数用法:1、使用“**kwarg”参数初始化字典;2、使用“mapping”参数初始化字典;3、使用“iterable”参数初始化字典;4、创建空字典。Python中的dict()函数用于创建一个字典对象,并可以进行键值对的初始化...
    99+
    2023-11-10
    python dict
  • python dict 取值方法
      在日常工作中,我们经常会遇到需要将一些数据转换为 dict格式的情况。比如: 1、想要将多个数组按照某种规则进行排列,形成有序的数据表,这时需要使用 dict函数。 3、想要将数据按照指定的方式进行存储,比如:按行存储、按列存储等,这...
    99+
    2023-09-10
    python 算法 开发语言
  • python dict
    1、Dictionary语法 Dictionary由key/value对(称为项目)组成,key和value之间用“:”分割,项目用“,”分割,所有项目用“{}”包括起来 >>> phonebook = {...
    99+
    2023-01-31
    python dict
  • python dict实现的魔法方法怎么用
    这篇文章主要介绍“python dict实现的魔法方法怎么用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“python dict实现的魔法方法怎么用”文章能帮助大家解决问题。方法说明__or__和_...
    99+
    2023-06-30
  • Python -- dict 类
    Python dict类常用方法:class dict(object):        def clear(self):  #清除字典中所有元素形成空字典,del是删除整个字典; >>> test {'k2': 'v2',...
    99+
    2023-01-31
    Python dict
  • python dict()方法学习笔记
    学习PYTHON 的dict()方法笔记。  dict() -> new empty dictionary |  dict(mapping) -> new dictionary initialized from a mappin...
    99+
    2023-01-31
    学习笔记 方法 python
  • python dict sorted 排
    python dict sorted 排序 转载自http://hi.baidu.com/jackleehit/blog/item/53da32a72207bafa9052eea1.html 我们知道Python的内置dictionary...
    99+
    2023-01-31
    python dict sorted
  • Python中的dict
    # dict # Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 d = {'Michael': 95, 'Bob': 75,...
    99+
    2023-01-31
    Python dict
  • Python字典dict
    dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。Python中的唯一一种映射类型。 举个例子,假设要根据同学的名字...
    99+
    2023-01-31
    字典 Python dict
  • python 字典dict
    # -*- coding: utf-8 -*- # ****************************** 创建 *************************** dict1 = {} #定义1个元素的字典 dict2 = ...
    99+
    2023-01-31
    字典 python dict
  • python字典 dict
    #dict 字典;是一种key:value的数据类型,没有下标,是无序的。字典可以嵌套任何类型,可以嵌套很多层。 #格式 dict1 = {     "name1":"123",     "name2":"456",     "name4"...
    99+
    2023-01-31
    字典 python dict
  • python的dict,set,list
    字典(dict)dict 用 {} 包围 dict.keys(),dict.values(),dict.items() hash(obj)返回obj的哈希值,如果返回表示可以作为dict的key del 或 dict.pop可以删除一个it...
    99+
    2023-01-31
    dict python list
  • python之dict与set
    dict全称dictionary,使用键-值(key-value)存储,书写一个dictname={:::} (name[])当数据量大时,字典比列表和元组速度快dict实现原理和查字典是一样的,假设字典包含一万字,list查询方法是一个一...
    99+
    2023-01-31
    python dict set
  • Python-Dict&Set类型
    Python的另外两种重要的数据类型Dict和Set,可以快速按照关键字检索信息 Dict - 字典 list 和 tuple 可以用来表示顺序集合,例如,班里同学的名字: ['Adam', 'Lisa', 'Bart'] 或者考试的...
    99+
    2023-01-31
    类型 Python Dict
  • python xml转成dict
    可以转成dict   defdictlist(node):     res={}     res[node.tag]={}     xmltodict(node,res[node.tag])    ...
    99+
    2023-01-31
    转成 python xml
  • Python对Dict排序
    对下面的Dict: aps = {} for key in T.keys(): ap = average_precision(T[key], P[key]) aps[key] = ap ...
    99+
    2023-01-31
    Python Dict
  • Python中字典dict
    字典是一种组合数据,没有顺序的组合数据,数据以键值对形式出现 # 字典的创建 # 创建空字典1 d = {} print(d) # 创建空字典2 d = dict() print(d) # 创建有值的字典, 每一组数据用冒号隔...
    99+
    2023-01-30
    字典 Python dict
  • 详解Python中的Dict
    目录什么是dict?我们下面看看dict的增删查改总结什么是dict? dict全称为dictionary(字典),人如其名,像字典一样可以根据索引定位到特定的文字。 在python...
    99+
    2022-11-12
  • python dict按照value 排
    我们知道Python的内置dictionary数据类型是无序的,通过key来获取对应的value。可是有时我们需要对dictionary中 的item进行排序输出,可能根据key,也可能根据value来排。到底有多少种方法可以实现对dict...
    99+
    2023-01-31
    python dict
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作