unity3d 词典访问

PYTHON字典指南 (PYTHON DICTIONARY GUIDE)

The dictionary is one of the data structures that are ready to use when programming in Python.

字典是使用Python进行编程时可以使用的数据结构之一。

在我们开始之前,什么是字典? (Before We Start, What is a Dictionary?)

Dictionary is an unordered and unordered Python collection that maps unique keys to some values. In Python, dictionaries are written by using curly brackets {} . The key is separated from the key by a colon : and every key-value pair is separated by a comma ,. Here’s how dictionaries are declared in Python.

字典是一个无序且无序的Python集合,它将唯一键映射到某些值。 在Python中,字典使用大括号{}编写。 的密钥是从一个冒号键分离:与每一个键-值对由逗号分隔, 。 这是在Python中声明字典的方式。

#A dictionary containing basketball players with their heights in mplayersHeight = {"Lebron James": 2.06,                 "Kevin Durant": 2.08,                  "Luka Doncic": 2.01,                 "James Harden": 1.96}

We have created the dictionary, however, what’s good about it if we cannot retrieve the data again right? This is where a lot of people do it wrong. I should admit, I was among them not long ago. After I realize the advantage, I never turn back even once. That’s why I am motivated to share it with you guys.

我们已经创建了字典,但是,如果我们不能再次正确检索数据,那有什么用呢? 这是很多人做错事的地方。 我应该承认,不久前我就是其中之一。 意识到优势之后,我再也不会回头一次。 这就是为什么我有动力与大家分享。

错误的方法 (The Wrong Way)

The well-known, or I should say the traditional way to access a value in a dictionary is by referring to its key name, inside a square bracket.

众所周知,或者我应该说在字典中访问值的传统方法是在方括号内引用其键名。

print(playersHeight["Lebron James"]) #print 2.06print(playersHeight["Kevin Durant"]) #print 2.08print(playersHeight["Luka Doncic"]) #print 2.01print(playersHeight["James Harden"]) #print 1.96

Everything seems OK, right? Not so fast! What do you think will happen if you type a basketball player’s name that is not in the dictionary? Look closely

一切似乎还好吧? 没那么快! 如果您键入词典中没有的篮球运动员的名字,您会怎么办? 仔细看

playersHeight["Kyrie Irving"] #KeyError 'Kyrie Irving'

Notice that when you want to access the value of the key that doesn’t exist in the dictionary will result in a KeyError. This could quickly escalate into a major problem, especially when you are building a huge project. Fret not! There are certainly one or two ways to go around this.

请注意,当您要访问字典中不存在的键的值时,将导致KeyError。 这可能很快会升级为一个主要问题,尤其是在构建大型项目时。 不用担心! 解决这个问题肯定有一两种方法。

Using If

使用If

if "Kyrie Irving" is in playersHeight:    print(playersHeight["Kyrie Irving"])

Using Try-Except

使用Try-Except

try:    print("Kyrie Irving")except KeyError as message:    print(message) #'Kyrie Irving'

Both snippets will run with no problem. For now, it seems okay, we can tolerate writing more lines to deal with the possibility of KeyError. Nevertheless, it will become annoying when the code you are writing is wrong.

两个片段都可以正常运行。 就目前而言,看起来还可以,我们可以忍受写更多的行来解决KeyError的可能性。 但是,当您编写的代码错误时,它将变得很烦人。

Luckily, there are better ways to do it. Not one, but two better ways! Buckle up your seat and get ready!

幸运的是,有更好的方法可以做到这一点。 不是一种,而是两种更好的方法! 系好安全带,准备好!

正确的方式 (The Right Way)

Using get() Method

使用get()方法

Using the get method is one of the best choices you can make when dealing with a dictionary. This method has 2 parameters, the first 1 is required while the second one is optional. However, to use the full potential of the get() method, I suggest you fill both parameters.

使用get方法是处理字典时最好的选择之一。 此方法有2个参数,第一个参数是必需的,第二个参数是可选的。 但是,要充分利用get()方法的潜力,建议您同时填写两个参数。

  • First: the name of the key which value you want to retrieve第一:键名要检索的值
  • Second: the value used if the key we are searching does not exist in the第二:如果要搜索的键不存在,则使用该值
#A dictionary containing basketball players with their heights in mplayersHeight = {"Lebron James": 2.06,                 "Kevin Durant": 2.08,                 "Luka Doncic": 2.01,                 "James Harden": 1.96}#If the key existsprint(playersHeight.get("Lebron James", 0)) #print 2.06print(playersHeight.get("Kevin Durant", 0)) #print 2.08#If the key does not existprint(playersHeight.get("Kyrie Irving", 0)) #print 0print(playersHeight.get("Trae Young", 0)) #print 0

When the key exists, get() method works exactly the same to referencing the name of the key in a square bracket. But, when the key does not exist, using get() method will print the default value we enter as the second argument.

当键存在时, get()方法的工作原理与在方括号中引用键的名称完全相同。 但是,当键不存在时,使用get()方法将打印我们输入的默认值作为第二个参数。

If you don’t specify the second value, a None value will be returned.

如果您未指定第二个值,则将返回None值。

You should also note that using the get() method will not modify the original dictionary. We will discuss it further later in this article.

您还应该注意,使用get()方法不会修改原始字典。 我们将在本文后面进一步讨论。

Using setdefault() method

使用setdefault()方法

What? There is another way? Yes of course!

什么? 还有另一种方法吗? 当然是!

You can use setdefault() method when you not only want to skip the try-except step but also overwrite the original dictionary.

当您不仅要跳过try-except步骤而且要覆盖原始字典时,可以使用setdefault()方法。

#A dictionary containing basketball players with their heights in mplayersHeight = {"Lebron James": 2.06,                 "Kevin Durant": 2.08,                 "Luka Doncic": 2.01,                 "James Harden": 1.96}#If the key existsprint(playersHeight.setdefault("Lebron James", 0)) #print 2.06print(playersHeight.setdefault("Kevin Durant", 0)) #print 2.08#If the key does not existprint(playersHeight.setdefault("Kyrie Irving", 0)) #print 0print(playersHeight.setdefault("Trae Young", 0)) #print 0

What I mean by overwriting is this, when you see the original dictionary again, you will see this.

我的意思是重写,当您再次看到原始词典时,您将看到此。

print(playersHeight)"""print{"Lebron James": 2.06, "Kevin Durant": 2.08, "Luka Doncic": 2.01, "James Harden": 1.96, "Kyrie Irving": 0, "Trae Young": 0}

Other than this feature, the setdefault() method is exactly similar to the get() method.

除了此功能外, setdefault()方法与get()方法完全相似。

最后的想法 (Final Thoughts)

Both get() and setdefault() are advanced techniques that you all must familiarize with. Implementing it is not hard and straightforward. The only barrier for you now is to break those old habits.

get()setdefault()都是您都必须熟悉的高级技术。 实现它并不困难和直接。 现在,您的唯一障碍是打破这些旧习惯。

However, I believe that as you use it, you will experience the difference immediately. After a while, you will no longer hesitate to change and start using get() and setdefault() methods.

但是,我相信,当您使用它时,您会立即体验到差异。 一段时间后,您将不再需要更改并开始使用get()setdefault()方法。

Remember, when you don’t want to overwrite the original dictionary, go with get() method.

请记住,当您不想覆盖原始字典时,请使用get()方法。

When you want to make changes to the original dictionary, setdefault() will be the better choice for you.

当您想更改原始字典时, setdefault()将是您的更好选择。

Regards,

问候,

Radian Krisno

拉迪安·克里斯诺(Radian Krisno)

翻译自: https://towardsdatascience.com/the-right-way-to-access-a-dictionary-2270e936443e

unity3d 词典访问


http://www.taodudu.cc/news/show-2338975.html

相关文章:

  • oracle11g查看数据库名称,oracle11g系列 事物和常用数据库对象
  • 武田呈报Mobocertinib治疗先前接受过含铂化疗的EGFR外显子20插入+ mNSCLC患者的阳性结果
  • matlab powf,科学网—MZDDE中操作数更正 - 张凯元的博文
  • 武田呈报mobocertinib的最新结果,进一步证实EGFR外显子20插入+ mNSCLC患者的临床收益
  • 无敌SQL
  • wheeltech惯导模块使用
  • Pytorch 操作整理
  • Java基础入门笔记
  • frida-trace入门
  • 【iOS逆向与安全】frida-trace入门
  • 三分钟教你学Git(十二) 之 fast-forward
  • Windows动态链接库DLL和静态库的原理以及创建方法
  • k3s 快速入门 - traefix 使用 - 1
  • k8s集群安装traefik 2.x (保证成功版)
  • 西班牙语动词变位探究:陈述式现在时
  • 中国最令人崩溃的25个姓氏,排名第1位的,打死都想不到
  • 爬虫综合大作业
  • 面试题:Redis 40 道
  • Redis 常见面试题(带答案)110道
  • 分享一个基于labview的2048小游戏(附详细教程+代码)
  • 面试 Redis 没底?这 40 道面试题让你不再慌
  • Redis面试题及答案 2021最新版 140道
  • 110道 Redis面试题及答案 (持续更新)
  • 爬虫综合
  • 面试大杂烩
  • Github 标星 3w+,热榜第一,使用 Python实现所有算法!
  • 适合小学生阅读的六本历史国学经典推荐。
  • 钮姓 历史来源
  • 「流程案例」| 胡润富豪榜数据获取、分析与可视化
  • HTML基础知识

unity3d 词典访问_正确的词典访问方式相关推荐

  1. 未声明spire。它可能因保护级别而不可访问_信息系统安全:访问控制技术概述...

    1.访问控制基本概念 身份认证技术解决了识别"用户是谁"的问题,那么认证通过的用户是不是可以无条件地使用所有资源呢?答案是否定的.访问控制(Access Control)技术就是用 ...

  2. mdx词典包_欧路词典—使用体验

    说起来用欧路词典也有大半年了,所说时间不长但是也够来给大家介绍一下这款超好用的词典 去年我买本柯林斯中阶词典,本来想买来查字典的,刚好冬天回到家抱着字典也回家,然后看英文小说(电脑上PDF),不认识的 ...

  3. python爬虫解决频繁访问_爬虫遇到IP访问频率限制的解决方案

    背景: 大多数情况下,我们遇到的是访问频率限制.如果你访问太快了,网站就会认为你不是一个人.这种情况下需要设定好频率的阈值,否则有可能误伤.如果大家考过托福,或者在12306上面买过火车票,你应该会有 ...

  4. jieba 词典 词频_在Hanlp词典和jieba词典中手动添加未登录词

    在使用Hanlp词典或者jieba词典进行分词的时候,会出现分词不准的情况,原因是内置词典中并没有收录当前这个词,也就是我们所说的未登录词,只要把这个词加入到内置词典中就可以解决类似问题,如何操作呢, ...

  5. apache2.4.9 开启path_info访问_如何通过SSH访问NAS?

    1.若是Windows用户,请先在电脑上安装支持SSH访问的工具,如putty.安装完成后,请为你的TNAS开启SSH访问. 2.前往控制面板-网络服务-Telnet与SNMP: 3.选择允许SSH访 ...

  6. redis 公网ip访问_怎样从公网访问内网Redis数据库

    公网访问内网Redis数据库 本地安装了Redis数据库,只能在局域网内访问,怎样从公网也能访问本地Redis数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Redis数据库 ...

  7. mdx词典包_欧路词典PC端 词库安装 渲染

    很早就购买了欧路词典(113元)的会员.还是很划算的.软件页很好用.可以安装词库,所以扩展性就≈无限了.不过我个人没有安装太多,主要用一本,就是牛津高阶双解(简体)第8版.在家里pc上安装后,效果很好 ...

  8. word拼写检查自定义词典下载_使用自定义词典进行拼写检查

    你做错的不是显式地更改任何文件中的任何内容.在 下面是一些代码来演示如何将内容写入文件...在fp = open(somefilepath,'w') 这一行打开一个要写入的文件,'w'告诉python ...

  9. wps拼写检查词典下载_如何将拼写检查仅限于Word中的主词典

    wps拼写检查词典下载 Word allows you to add custom dictionaries to use when checking spelling. When you run t ...

  10. word拼写检查自定义词典下载_如何在Word中限制拼写检查到主词典 | MOS86

    Word允许您添加自定义字典以在检查拼写时使用.当您运行拼写检查器或Word自动检查拼写输入时,文档中的单词将与主词典和您可能添加的任何自定义词典进行比较. 相关文章图片1tupian如何在Word ...

最新文章

  1. 如何选择合适的损失函数,请看......
  2. 列标题 如何删除gridcontrol_DEV控件GridControl常用属性设置(转)
  3. TensorFlow 笔记6--迁移学习
  4. 网易云信联合墨刀,邀你参加【产品设计狂欢节】!
  5. 外设驱动库开发笔记10:SHT2x系列温湿度传感器驱动
  6. java面试关于ssh的_[Java教程]ssh面试题
  7. Android逆向基础笔记—Android中的常用ARM汇编指令
  8. eclipse linux 中文,Eclipse (简体中文)
  9. 注销、重启、关机快捷键命令
  10. Hive内表和外表浅析
  11. 10bit视频编码——特性及全面播放方法介绍
  12. 用了服务器后网站统计代码被劫持,网站劫持代码,网站被劫持怎么办 | 帮助信息-动天数据...
  13. 程序员写个人技术博客的价值与意义
  14. Whitelabel Error Page 的原因
  15. python123查找指定字符输入m_Pyton学习—字符串
  16. oracle11g連不上em,oracle11gem重建失败的几点解决办法.doc
  17. 免费视频格式转换软件,6大免费视频转换器推荐
  18. 解决页面刷新数据丢失,数据持久化问题
  19. QuickBooks 2020 for Mac(mac财务管理软件)
  20. 前端工程化 - 剖析npm的包管理机制

热门文章

  1. Rails进阶——框架理论认知与构建方案建设(一)
  2. 测量运放的输入偏置电流 - 实验准备
  3. Linux磁盘阵列(RAID0、RAID1、RADI5、 RAID6、RAID1+0)
  4. Telink zigbee射频和功耗测试的方法
  5. 3A算法—自动曝光(AE)
  6. 一次性补助20万,博士买房比市价低1.5万/平!26城硕博引进政策哪家强?
  7. html文档 word文档,html文档怎么转Word文档
  8. 一年月份大小月口诀_农历大小月卦口诀详解(最新版).doc
  9. 基于51单片机的7键8键电子琴proteus仿真数码管显示程序原理设计
  10. Windows11如何使用安卓子系统的Amazon Appstore