郑州高端网站建设是什么意思,用户登录界面设计,用php做电子商务网站,从化做网站unity3d 词典访问Python字典指南 (Python Dictionary Guide) The dictionary is one of the data structures that are ready to use when programming in Python.字典是使用Python进行编程时可以使用的数据结构之一。 在我们开始之前#xff0c;什么是字典#xff1f; (Bef…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.96Everything 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 IrvingNotice 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 IrvingBoth 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 0When 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 0What 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-7a19b064e62bunity3d 词典访问