ui自动化解决图形验证码
This article provides a beginner suitable intro to defaultdict module of collections .The author assumes that you are familiar with dict in python.
本文为集合的defaultdict模块提供了适合初学者的介绍。作者假定您熟悉python中的dict。
What is the difference between defaultdict and dict ? . A defaultdict works exactly the same way a normal dict does, but it is initialized with a callable function called default_factory() that takes no arguments and provides a default value for a non-existent key.
defaultdict和dict有什么区别? 。 defaultdict的工作原理与普通dict完全相同,但是它使用名为default_factory()的可调用函数进行了初始化,该函数不包含任何参数,并为不存在的键提供默认值。
Let’s look at a fancy example . Imagine in a office the salary of nearly every employee is 5000 $ . But some experienced employees get a different amount which is specified by the manager .
让我们看一个花哨的例子。 想象在办公室里几乎每个雇员的工资是5000美元。 但是一些经验丰富的员工得到的金额由经理指定。
The output of the code snippet would look like :
代码段的输出如下所示:
600070005000
600070005000
Note that we previously didn’t add any key named Cooley in the dictionary named “salary”.
请注意,我们以前没有在名为“ salary”的字典中添加任何名为Cooley的键。
So, what is going inside ?
那么,里面发生了什么?
If we had done the same with general dictionary , then it would raise a key error like this :
如果我们对通用字典做了同样的事情,那么它将引发一个关键错误,如下所示:
Now what is defaultdict doing ? It is assigning a default value to any unassigned new key of the dictionary by a function .
现在defaultdict在做什么? 它通过函数为字典的任何未分配的新键分配默认值。
As we didn’t define any key named “Cooley” , the defaultdict assigned the default value i.e the value we declared in function “func” above. This is the specialty of defaultdict and this feature very often comes handy while working with graph problems.
因为我们没有定义任何名为“ Cooley”的键,所以defaultdict分配了默认值,即我们在上面的函数“ func”中声明的值。 这是defaultdict的特长,在处理图形问题时,此功能通常很方便。
总结一下:defaultdict为不存在的密钥提供一个默认值。 这样就避免了关键错误。 (To sum up : defaultdict provides a default value for the key that does not exists. Thus the key error is averted .)
Let us look at another example to make this even clearer . Imagine we need to need to make a lookup table named menu where the key is the country’s name and the value is the list of foods originated in this country . The key will be a string and the value will be a list.
让我们看另一个例子,以使这一点更加清楚。 想象一下,我们需要创建一个名为menu的查找表,其中的键是该国家的名称,而值是该国家的食品清单。 键将是一个字符串,值将是一个列表。
from collections import defaultdictmenu = defaultdict(list)menu['Italy']=['Pizza','Pasta']
menu['Mexico']=['Taco']
menu['Japan']=['Ramen']print(menu['Italy'])
print(menu['Mexico'])
print(menu['Japan'])
print(menu['Iceland'])
print(menu['Poland'])
The output will be as follows :
输出将如下所示:
['Pizza', 'Pasta']
['Taco']
['Ramen']
[]
[]
Note that we did not define any key named ‘Iceland’ and ‘Poland’ in the menu defaultdict.
注意,我们没有在菜单 defaultdict中定义任何名为“ Iceland”和“ Poland”的键。
In line 2 of the code snippet , we defined the the default function as an empty list. i.e. :
在代码段的第2行中,我们将默认功能定义为空列表。 即:
menu = defaultdict(list)
So any key that was not assigned before will show an empty list as default value.
因此,之前未分配的任何键都将显示一个空列表作为默认值。
翻译自: https://medium.com/@rafat.joy99/from-grokking-defaultdict-to-solving-graph-problems-b08b4fb9d637
ui自动化解决图形验证码