本篇文章给大家带来的内容是关于pytho中字典操作方法介绍(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
字典,名称就叫做dictionary,翻译过来是字典,类似于前面的int/str/list,这种类型数据名称是:dict
实验:
>>>help(dict)
使用dir,也能得到相同的结果。
>>> dir(dict) ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
values这个内置函数的使用方法
>>> help(dict.values)
Help on method_descriptor: values(...) D.values() -> list of D's values (END)
q键退回。
概述
python中的dict具有如下特点:
dict是可变的
dict可以存储任意数量的Python对象
dict可以存储任何python数据类型
dict以:key:value,即“键:值”对的形式存储数据,每个键是唯一的。
dict也被称为关联数组或哈希表。
以上诸条,如果还不是很理解,也没有关系,通过下面的学习,特别是通过各种实验,就能理解了。
创建dict
方法1:
创建一个空的dict,这个空dict,可以在以后向里面加东西用。
>>> mydict = {} >>> mydict {}
创建有内容的dict。
>>> person = {"name":"hiekay","site":"hiekay.github.io","language":"python"} >>> person {'name': 'hiekay', 'language': 'python', 'site': 'hiekay.github.io'}
“name”:"hiekay"就是一个键值对,前面的name叫做键(key),后面的hiekay是前面的键所对应的值(value)。在一个dict中,键是唯一的,不能重复;值则是对应于键,值可以重复。键值之间用(:)英文的分号,每一对键值之间用英文的逗号(,)隔开。
>>> person['name2']="hiekay" #这是一种向dict中增加键值对的方法 >>> person {'name2': 'hiekay', 'name': 'hiekay', 'language': 'python', 'site': 'hiekay.github.io'}
如下,演示了从一个空的dict开始增加内容的过程:
>>> mydict = {} >>> mydict {} >>> mydict["site"] = "hiekay.github.io" >>> mydict[1] = 80 >>> mydict[2] = "python" >>> mydict["name"] = ["zhangsan","lisi","wangwu"] >>> mydict {1: 80, 2: 'python', 'site': 'hiekay.github.io', 'name': ['zhangsan', 'lisi', 'wangwu']} >>> mydict[1] = 90 #如果这样,则是修改这个键的值 >>> mydict {1: 90, 2: 'python', 'site': 'hiekay.github.io', 'name': ['zhangsan', 'lisi', 'wangwu']}
方法2: 元组
>>> name = (["first","Google"],["second","Yahoo"]) #这是另外一种数据类型,称之为元组,后面会讲到 >>> website = dict(name) >>> website {'second': 'Yahoo', 'first': 'Google'}
方法3: Python 字典 fromkeys() 函数用于创建一个新字典,以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值。
>>> website = {}.fromkeys(("third","forth"),"facebook") >>> website {'forth': 'facebook', 'third': 'facebook'}
需要提醒的是,这种方法是从新建立一个dict。
访问dict的值
因为dict是以键值对的形式存储数据的,所以,只要知道键,就能得到值。这本质上就是一种映射关系。
>>> person {'name2': 'hiekay', 'name': 'hiekay', 'language': 'python', 'site': 'hiekay.github.io'} >>> person['name'] 'hiekay' >>> person['language'] 'python' >>> site = person['site'] >>> print site hiekay.github.io
遍历:
可以用for语句
>>> person {'name2': 'hiekay', 'name': 'hiekay', 'language': 'python', 'site': 'hiekay.github.io'} >>> for key in person: ... print person[key] ... hiekay hiekay python hiekay.github.io
以上是pytho中字典操作方法介绍(代码示例)的详细内容。更多信息请关注PHP中文网其他相关文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

数组的同质性对性能的影响是双重的:1)同质性允许编译器优化内存访问,提高性能;2)但限制了类型多样性,可能导致效率低下。总之,选择合适的数据结构至关重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,内存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

Atom编辑器mac版下载
最流行的的开源编辑器