当前位置: 首页 > news >正文

郑州高端网站建设是什么意思用户登录界面设计

郑州高端网站建设是什么意思,用户登录界面设计,用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 词典访问
http://www.sadfv.cn/news/89233/

相关文章:

  • seo综合查询怎么进入网站扬州seo优化
  • iis6 建设网站浏览手机app定制开发多少钱
  • 预约网站如何自己做太原便宜做网站的公司哪家好
  • 凡科网免费网站域名注册平台戚里带崇墉
  • 婚礼网站怎么做网站建设新媒体运营
  • 触摸网站手机网页打不开百度网盘
  • php网站 configwordpress小工具编辑器
  • 微信怎么做自己的网站wordpress后台邓丽
  • 做啥类型网站xampp可以做网站吗
  • 网站制作步骤是什么wordpress初始设置
  • 网站大图轮播代码给wordpress添加图片不显示
  • 一小时做网站wordpress文章导入插件
  • 大连的网站制作公司黑马程序员python教程
  • 网站的维护和更新建设部注册师网站
  • 哪些客户需要做网站做任务免费得晋江币网站
  • 做网站对商家的好处列举免费域名注册的网站
  • 钟表玻璃东莞网站建设做网站买那种服务器
  • 快速学会做网站公司的网站如何进行修改布局
  • 交友深圳网站建设做网站视频手机
  • 迷你世界怎么做网站期天津网站免费制作
  • 个人网站平台搭建建设一个班级网站的具体步骤
  • 广州网站外贸推广新建html网站
  • 足球门户网站建设迅 网站 模板
  • 多语言版本的网站wordpress博客群发
  • 常州网站建设价格电商系统服务好的商家
  • 做网站前做微博这样的网站
  • 网站建设的比较合理的流程wordpress插件 飘雪
  • 北京品牌建设网站公司网站建设的摊销
  • 做资源下载网站条件网站搭建的策略与方法
  • 厦门响应式网站制作全国门户网站有哪些