Python check that key is defined in dictionary

This question already has an answer here: Check if a given key already exists in a dictionary 18 answers Use the in operator: if b in a: Demo: >>> a = {'foo': 1, 'bar': 2} >>> 'foo' in a True >>> 'spam' in a False You really want to start reading the Python tutorial, the section on dictionaries covers this very subject. Its syntax is if key in dict: : if "b"

Python检查密钥是否在字典中定义

这个问题在这里已经有了答案: 检查给定的密钥是否已经存在于字典中18个答案 使用in运算符: if b in a: 演示: >>> a = {'foo': 1, 'bar': 2} >>> 'foo' in a True >>> 'spam' in a False 你真的想开始阅读Python教程,字典部分涵盖了这个主题。 它的语法是if key in dict: : if "b" in a: a["b"] += 1 else: a["b"] = 1 现在你可能想看看collections.defaultdict和(对于上面

How to test if a dictionary contains a specific key?

Possible Duplicate: What is a good way to test if a Key exists in Python Dictionary What's the cleanest way to test if a dictionary contains a key? x = {'a' : 1, 'b' : 2} if (x.contains_key('a')): .... 'a' in x 并快速搜索揭示了一些关于它的好信息:http://docs.python.org/3/tutorial/datastructures.html#dictionaries

如何测试字典是否包含特定的密钥?

可能重复: 测试Python Dictionary中是否存在Key的好方法是什么? 测试字典是否包含密钥的最简洁方法是什么? x = {'a' : 1, 'b' : 2} if (x.contains_key('a')): .... 'a' in x 并快速搜索揭示了一些关于它的好信息:http://docs.python.org/3/tutorial/datastructures.html#dictionaries

Python: how can I check if the key of an dictionary exists?

Possible Duplicate: What is a good way to test if a Key exists in Python Dictionary Let's say I have an associative array like so: {'key1': 22, 'key2': 42} . How can I check if key1 exists in the dictionary? if key in array: # do something 关联数组在Python中称为字典,您可以在stdtypes文档中了解更多关于它们的信息。 另一种方法是has_key()(如果仍然使用2.X) >>>

Python:我如何检查字典的键是否存在?

可能重复: 测试Python Dictionary中是否存在Key的好方法是什么? 假设我有一个像这样的关联数组: {'key1': 22, 'key2': 42} 。 如何检查字典中是否存在key1 ? if key in array: # do something 关联数组在Python中称为字典,您可以在stdtypes文档中了解更多关于它们的信息。 另一种方法是has_key()(如果仍然使用2.X) >>> a={"1":"one","2":"two"} >>> a.has_key("1") True

Indentation in python GUI

As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something... eg: def somefunction: x =5 return x if i want to add a contrl block eg: def somefunctio

在python GUI中缩进

当我在python中编写代码时,突然觉得在代码前面添加了一个新的代码段,我已经写完了......完整代码的缩进会受到影响..移动到每一行并将其更改为非常繁琐的过程缩进...有没有办法做自动缩进或什么的... 例如: def somefunction: x =5 return x 如果我想添加一个控制块 例如: def somefunction: if True: x =5 return x return 0 添加控制块的这个小改变花了很多标签工作....

Python Lambda in a loop

Considering the following code snippet : # directorys == {'login': <object at ...>, 'home': <object at ...>} for d in directorys: self.command["cd " + d] = (lambda : self.root.change_directory(d)) I expect to create a dictionary of two function as following : # Expected : self.command == { "cd login": lambda: self.root.change_directory("login"), "cd home": lambda: self.

Python Lambda在循环中

考虑下面的代码片段: # directorys == {'login': <object at ...>, 'home': <object at ...>} for d in directorys: self.command["cd " + d] = (lambda : self.root.change_directory(d)) 我期望创建一个两个函数的字典,如下所示: # Expected : self.command == { "cd login": lambda: self.root.change_directory("login"), "cd home": lambda: self.root.change_directory("home") } 但它看起

What is the scope of a defaulted parameter in Python?

When you define a function in Python with an array parameter, what is the scope of that parameter? This example is taken from the Python tutorial: def f(a, L=[]): L.append(a) return L print f(1) print f(2) print f(3) Prints: [1] [1, 2] [1, 2, 3] I'm not quite sure if I understand what's happening here. Does this mean that the scope of the array is outside of the function?

Python中的默认参数的范围是什么?

当你用一个数组参数在Python中定义一个函数时,该参数的范围是什么? 这个例子来自Python教程: def f(a, L=[]): L.append(a) return L print f(1) print f(2) print f(3) 打印: [1] [1, 2] [1, 2, 3] 我不太确定,如果我明白这里发生了什么。 这是否意味着数组的作用域在函数之外? 为什么数组会记住呼叫时的值? 来自其他语言,只要变量是静态的,我就会期待这种行为。 否则,似乎应该每次都重置。 实际

Local variables in Python nested functions

Okay, bear with me on this, I know it's going to look horribly convoluted, but please help me understand what's happening. from functools import partial class Cage(object): def __init__(self, animal): self.animal = animal def gotimes(do_the_petting): do_the_petting() def get_petters(): for animal in ['cow', 'dog', 'cat']: cage = Cage(animal) def pe

Python嵌套函数中的局部变量

好吧,对我承担这一点,我知道这将看起来非常令人费解,但请帮助我了解发生了什么。 from functools import partial class Cage(object): def __init__(self, animal): self.animal = animal def gotimes(do_the_petting): do_the_petting() def get_petters(): for animal in ['cow', 'dog', 'cat']: cage = Cage(animal) def pet_function(): print "Mary pets the " + c

Instantiation time for mutable default arguments of closures in Python

My understanding is that when Python parses the source code of a function, it compiles it to bytecode but doesn't run this bytecode before the function is called (which is why illegal variable names in functions does not throw an exception unless you call the function). Default arguments are not instantiated during this initial setup of the function, but only when the function is called for

Python中闭包的可变默认参数的实例化时间

我的理解是,当Python解析函数的源代码时,它会将其编译为字节码,但在函数调用之前不会运行此字节码(这就是为什么函数中的非法变量名称除非调用函数才会抛出异常)。 默认参数在初始化函数初始化期间没有被实例化,但是只有当函数被第一次调用时,不管参数是否被提供。 默认参数的这个相同实例用于将来的所有调用,这可以通过使用可变类型作为默认参数来看到。 但是,如果我们将函数放入另一个函数中,则每次调用外部函数

Strange behavior with a list as a default function argument

Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument List extending strange behaviour Pyramid traversal view lookup using method names Let's say I have this function: def a(b=[]): b += [1] print b Calling it yields this result: >>> a() [1] >>> a() [1, 1] >>> a() [1, 1, 1] When I change b += [1] to b = b + [1] , the beh

将列表作为默认函数参数的奇怪行为

可能重复: Python中的“最小惊讶”:可变的默认参数 列表扩展奇怪的行为 使用方法名称的金字塔遍历视图查找 假设我有这个功能: def a(b=[]): b += [1] print b 调用它会产生这样的结果: >>> a() [1] >>> a() [1, 1] >>> a() [1, 1, 1] 当我将b += [1]更改为b = b + [1] ,函数的行为会发生变化: >>> a() [1] >>> a() [1] >>> a() [1] b = b + [1

Python variable scope error

The following code works as expected in both Python 2.5 and 3.0: a, b, c = (1, 2, 3) print(a, b, c) def test(): print(a) print(b) print(c) # (A) #c+=1 # (B) test() However, when I uncomment line (B) , I get an UnboundLocalError: 'c' not assigned at line (A) . The values of a and b are printed correctly. This has me completely baffled for two reasons: Why i

Python变量范围错误

以下代码在Python 2.5和3.0中都能按预期工作: a, b, c = (1, 2, 3) print(a, b, c) def test(): print(a) print(b) print(c) # (A) #c+=1 # (B) test() 但是,当我取消注释行(B)时 ,我得到一个UnboundLocalError: 'c' not assigned在行(A)处UnboundLocalError: 'c' not assigned 。 a和b的值被正确打印。 这让我完全困惑,原因有两个: 为什么由于线(B)后面的语句会在