dict函数
基本的字典操作
len(d)返回d中项(key-value)的数量
d[k]返回关联到键k上的值
d[k]=v将值v关联到键k上
del d[k]删除键为k的项
k in d检查d中是否有含有键为k的项
来看个简单的例子
databases = { "Oracle" : { "username" : "oracleapp", "password" : "oracleapp123" }, "Db2" : { "username" : "db2app", "password" : "db2app123" }, "MongoDB" : { "username" : "mongodbapp", "password" : "mongodbapp123" } } labels = { "username" : "DB Username", "password" : "DB Password" } name = raw_input("DB Name : ") request = raw_input("DB Username(u) or DB Password(p)? ") if request == "u" : key = "username" if request == "p" : key = "password" if name in databases : print "%s database's %s is %s." % (name, labels[key], databases[name][key])
运行结果如下所示:
字典的格式化字符串
字典方法:clear、copy、fromkeys、get
接下来改写上面那个代码,使用get方法来访问实体
databases = { "Oracle" : { "username" : "oracleapp", "password" : "oracleapp123" }, "DB2" : { "username" : "db2app", "password" : "db2app123" }, "MongoDB" : { "username" : "mongodbapp", "password" : "mongodbapp123" } } labels = { "username" : "DB Username", "password" : "DB Password" } name = raw_input("DB Name : ") request = raw_input("DB Username(u) or DB Password(p)? ") if request == "u" : key = "username" elif request == "p" : key = "password" else : key = request db = databases.get(name, {}) label = labels.get(key, key) result = db.get(key, "not available") print "%s database's %s is %s." % (name, label, result)
运行结果如下所示:
has_key、items、iteritems、keys、iterkeys、values、itervalues
pop、popitem、setdefault、update
Summary
相关推荐
标题"Python-Traversable Python Dictionaries"指的是深入探讨如何遍历和操作Python字典。在本篇文章中,我们将详细讨论Python字典的特性和遍历方法,以及在实际编程中如何有效地使用它们。 首先,Python字典的创建...
Python字典概述在本课程中,我们将学习Python dictionaries ! 像JavaScript中的对象一样,Python中的字典是一种功能强大的数据类型,它使我们可以存储键和值。目标了解如何创建和格式化字典了解如何修改和阅读词典...
A namespace is a mapping from names to objects.Most namespaces are currently implemented as Python dictionaries。 命名空间(Namespace)是从名称到对象的映射,大部分的命名空间都是通过 Python 字典来实现的...
"nested-dictionaries-0.0.1.tar.gz" 是一个针对Python的库,专门处理嵌套字典的工具,其版本号为0.0.1,以tar.gz格式压缩打包。 嵌套字典是Python字典数据结构的一个扩展,允许在一个字典中包含其他字典,形成多层...
在Python中,变量的创建需要赋值,变量类型包括Numbers(数字)、Strings(字符串)、Lists(列表)、Tuples(元组)和Dictionaries(字典)等。数字类型有int、float、long和complex,其中,Python 3.x中long类型与...
第七部分,"Keeping Dictionaries in Order",讨论了在Python 3.7之前的版本中,字典是无序的,而从Python 3.7开始,字典保持了插入顺序。如果需要一个有序的字典并且兼容旧版本的Python,可以使用`collections....
接下来是更高级的主题,如列表(lists)、元组(tuples)、字典(dictionaries)和集合(sets)。这些是Python中的内置数据结构,它们在处理数据时非常有用。案例可能包括元素的添加、删除、查找和排序等操作。同时,你可能...
- Explore Python’s major built-in object types such as numbers, lists, and dictionaries - Create and process objects with Python statements, and learn Python’s general syntax model - Use functions ...
Dictionaries Control flow Functions Modules and scoping rules Python programs Using the filesystem Reading and writing files Exceptions PART 3 - ADVANCED LANGUAGE FEATURES Classes and object-oriented ...
**字典(Dictionaries)**: 字典是Python的关联数组,由键值对组成,使用花括号`{}`定义。它通过键来查找对应的值,键必须是唯一的,但值可以重复。字典在处理数据映射时非常有用,例如定义一个学生信息字典: ```...
Variables, strings, lists, dictionaries and conditional statements are and how to use them Some of the standard libraries and what they can do to help you How to write your own functions How to write ...
•Sets, Lists, and Dictionaries, and when to use each •Gathering keyboard input •How to define a class •Looping constructs •Handling Exceptions in code •Slicing sequences •Creating modular...
- **变量赋值的顺序保持不变(Insertion Order for Dictionaries)**:从3.7开始,字典保留插入顺序,这是一个向后兼容的改变,对于依赖字典顺序的代码很有帮助。 - **类型注解(Type Annotations)**:3.7增强了...
Dictionaries, lists, tuples, and other data structures specific to Python receive plenty of attention including complete examples., Authors Mark Lutz and David Ascher build on that fundamental ...
2. **高效的数据结构**:Python内建了多种高效的数据结构,如列表(lists)、元组(tuples)、字典(dictionaries)等,这些结构能够极大地提高数据处理的效率。 3. **面向对象编程**:Python支持面向对象的编程方式,用户...
You will also learn about: Programs in Files, variables and Strings Loops and Conditional Statements While Loops Boolean Operators Conditional Statements Functions Tuples, Lists, and Dictionaries ...
PEP 372: Ordered Dictionaries PEP 378: Format Specifier for Thousands Separator Other Language Changes New, Improved, and Deprecated Modules Optimizations IDLE Build and C API Changes Porting ...