Python Dict用法

简介:

#字典的添加、删除、修改操作

dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}

dict["w"] = "watermelon"

del(dict["a"])

dict["g"] = "grapefruit"

print dict.pop("b")

print dict

dict.clear()

print dict

#字典的遍历

dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}

for k in dict:

    print "dict[%s] =" % k,dict[k]

#字典items()的使用

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}

#每个元素是一个key和value组成的元组,以列表的方式输出

print dict.items()

#调用items()实现字典的遍历

dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}

for (k, v) in dict.items():

    print "dict[%s] =" % k, v

#调用iteritems()实现字典的遍历

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}

print dict.iteritems()

for k, v in dict.iteritems():

    print "dict[%s] =" % k, v

for (k, v) in zip(dict.iterkeys(), dict.itervalues()):

    print "dict[%s] =" % k, v

   


#使用列表、字典作为字典的值

dict = {"a" : ("apple",), "bo" : {"b" : "banana", "o" : "orange"}, "g" : ["grape","grapefruit"]}

print dict["a"]

print dict["a"][0]

print dict["bo"]

print dict["bo"]["o"]

print dict["g"]

print dict["g"][1]

 

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}

#输出key的列表

print dict.keys()

#输出value的列表

print dict.values()

#每个元素是一个key和value组成的元组,以列表的方式输出

print dict.items()

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}

it = dict.iteritems()

print it

#字典中元素的获取方法

dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}

print dict

print dict.get("c", "apple")         

print dict.get("e", "apple")

#get()的等价语句

D = {"key1" : "value1", "key2" : "value2"}

if "key1" in D:

    print D["key1"]

else:

    print "None"

#字典的更新

dict = {"a" : "apple", "b" : "banana"}

print dict

dict2 = {"c" : "grape", "d" : "orange"}

dict.update(dict2)

print dict

#udpate()的等价语句

D = {"key1" : "value1", "key2" : "value2"}

E = {"key3" : "value3", "key4" : "value4"}

for k in E:

    D[k] = E[k]

print D

#字典E中含有字典D中的key

D = {"key1" : "value1", "key2" : "value2"}

E = {"key2" : "value3", "key4" : "value4"}

for k in E:

    D[k] = E[k]

print D

#设置默认值

dict = {}

dict.setdefault("a")

print dict

dict["a"] = "apple"

dict.setdefault("a","default")

print dict

#调用sorted()排序

dict = {"a" : "apple", "b" : "grape", "c" : "orange", "d" : "banana"}

print dict  

#按照key排序 

print sorted(dict.items(), key=lambda d: d[0])

#按照value排序 

print sorted(dict.items(), key=lambda d: d[1])

#字典的浅拷贝

dict = {"a" : "apple", "b" : "grape"}

dict2 = {"c" : "orange", "d" : "banana"}

dict2 = dict.copy()

print dict2


#字典的深拷贝

import copy

dict = {"a" : "apple", "b" : {"g" : "grape","o" : "orange"}}

dict2 = copy.deepcopy(dict)

dict3 = copy.copy(dict)

dict2["b"]["g"] = "orange"

print dict

dict3["b"]["g"] = "orange"

print dict





     本文转自aaron428 51CTO博客,原文链接:http://blog.51cto.com/aaronsa/1783324,如需转载请自行联系原作者

相关文章
|
1月前
|
Python
请解释Python中的递归是什么?并举例说明其用法。
【2月更文挑战第25天】【2月更文挑战第85篇】请解释Python中的递归是什么?并举例说明其用法。
|
1月前
|
Python
Python中的装饰器是什么?请举例说明其用法。
【2月更文挑战第24天】【2月更文挑战第77篇】Python中的装饰器是什么?请举例说明其用法。
|
1月前
|
测试技术 Python
Python中的装饰器:概念、用法及实例
【2月更文挑战第25天】 装饰器在Python中是一种强大的工具,它允许我们在不修改原始函数代码的情况下,增加函数的功能。本文将深入探讨装饰器的概念,解析其工作原理,并通过实例来展示如何在Python中使用装饰器。
|
1月前
|
存储 缓存 Python
Python中的装饰器:概念、用法和实例
【2月更文挑战第25天】 在Python编程中,装饰器是一种强大的工具,它允许我们在不修改原始函数代码的情况下,增加或修改函数的行为。本文将深入探讨装饰器的概念、用法以及通过实例来使其更易于理解。我们将看到,装饰器不仅仅是语法糖,而是一种可以极大提高代码复用性和可读性的有效工具。
|
1月前
|
Python
Python 中的装饰器:概念、用法和实例
【2月更文挑战第23天】 在编程世界中,装饰器是一种强大的工具,它允许我们在不改变现有代码的情况下增加或修改函数和类的行为。本文将深入探讨 Python 中装饰器的概念、用法以及如何创建自定义装饰器。我们将通过实例来演示装饰器的实用性和灵活性,帮助读者更好地理解和应用这一技术。
|
10天前
|
Python
python中threads.append的用法
将线程对象`t`添加到`threads`列表便于管理与控制线程,如等待所有线程完成。通过迭代列表并调用`join`方法,可依次等待每个线程执行完毕,实现同步。代码示例: ```python for t in threads: t.join() print("All threads are done!") ``` `join`方法使当前线程阻塞,直到线程执行结束。所有线程完成后,输出"All threads are done!"。
14 1
|
1月前
|
Python
python函数用法(五)
python函数用法(五)
23 1
|
1月前
|
Python
python函数用法(四)
python函数用法(四)
21 0
|
1月前
|
自然语言处理 Python
python函数用法(三)
python函数用法(三)
18 0
|
1月前
|
Python
python函数用法(二)
python函数用法(二)
18 0

热门文章

最新文章