鱼C论坛

 找回密码
 立即注册
查看: 1364|回复: 1

[技术交流] python 026 对字典进行操作

[复制链接]
发表于 2018-6-24 01:09:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 风不会停息 于 2018-6-24 01:50 编辑

dict1 = {}

1. dict1.fromkeys(iterable, value=None), 创建一个或多个键, 并赋予相同的值, 如果没有传入 value 参数, 则默认 value = none, 例如:

  1. >>> dict1 = {}
  2. >>> dict1.fromkeys( (1, 2, 3), 'hello' )
  3. {1: 'hello', 2: 'hello', 3: 'hello'}
复制代码

  1. >>> dict1 = {}
  2. >>> dict1.fromkeys( (1, 2, 3) )
  3. {1: None, 2: None, 3: None}
复制代码


2. dict1.keys(), 多用来和for使用来遍历字典中的键, 例如:

  1. >>> dict1 = {1: 'hello', 2: 'hello', 3: 'hello'}
  2. >>> for  eachkey in dict1.keys:
  3.         print(eachkey, end = ' ')

  4. 1 2 3
复制代码


3. dict1.values(), 多用来和for使用来遍历字典中的值, 例如:

  1. >>> dict1 = {1: 'hello', 2: 'hello', 3: 'hello'}
  2. >>> for  eachvalue in dict1.values():
  3.         print(eachvalue, end = ' ')

  4. hello hello hello
复制代码


4. dict1.items(), 多用来和for使用来遍历字典中的项, 例如:

  1. >>> dict1 = {1: 'hello', 2: 'hello', 3: 'hello'}
  2. >>> for  eachitem in dict1.items():
  3.         print(eachitem, end = ' ')

  4. (1, 'hello') (2, 'hello') (3, 'hello')
复制代码


5. dict1.clear(), 清空字典, dict1 = {}

6. dict2 = dict1.copy(),将 dict1 拷贝给 dict2, 使用这种方法dict1 和 dict2 地址不同, 若使用 dict2 = dict1, 则dict1 和dict2 的地址相同

7. dict1.get(key [,value]), 查找 key 是否存在在 dict1 中, 若存在则打印dict1[key], 若不存在则返回None, 若传入value参数, 则返回value, 例如:

  1. >>> dict1 = {1: 'hello', 2: 'hello', 3: 'hello'}
  2. >>> dict1.get(1, 'love')
  3. 'hello'
  4. >>> dict1
  5. {1: 'hello', 2: 'hello', 3: 'hello'}
  6. >>> dict1.get(5, 'hello')
  7. 'hello'
  8. >>> dict1
  9. {1: 'hello', 2: 'hello', 3: 'hello'}
  10. >>>
复制代码


8. dict1.pop(key), 返回dict1中的key对应的值并删除该项, 例如:

  1. >>> dict1 = {1: 'hello', 2: 'hello', 3: 'hello'}
  2. >>> dict1.pop(3)
  3. 'hello'
  4. >>> dict1
  5. {1: 'hello', 2: 'hello'}
复制代码


9. dict1.popitem(), 返回dict1中随机一项(不一定是最后一项)并删除该项, 例如:

  1. >>> dict1 = dict1.fromkeys( range(10), 'hello' )
  2. >>> dict1
  3. {0: 'hello', 1: 'hello', 2: 'hello', 3: 'hello', 4: 'hello', 5: 'hello', 6: 'hello', 7: 'hello', 8: 'hello', 9: 'hello'}
  4. >>> dict1.popitem()
  5. (9, 'hello')
  6. >>> dict1
  7. {0: 'hello', 1: 'hello', 2: 'hello', 3: 'hello', 4: 'hello', 5: 'hello', 6: 'hello', 7: 'hello', 8: 'hello'}
复制代码


10. dict1.setdefault(key [,value]), 类似dict1.get(), 当字典中不存在key时会将其加入字典中并默认其值为None, 若传入了value参数, 则将其值改为value, 不能对已经存在的项进行修改, 例如:

  1. >>> dict1 = {1: 'hello', 2: 'hello', 3: 'hello'}
  2. >>> dict1.setdefault(5)
  3. >>> dict1
  4. {1: 'hello', 2: 'hello', 3: 'hello', 5: None}
  5. >>> dict1.setdefault(4, 'hello')
  6. 'hello'
  7. >>> dict1
  8. {1: 'hello', 2: 'hello', 3: 'hello', 5: None, 4: 'hello'}
复制代码


11. dict1.update(dict2), 按照dict2对dict1进行更新, 例如:

  1. >>> dict1 = dict1.fromkeys( range(1, 4) )
  2. >>> dict1
  3. {1: None, 2: None, 3: None}
  4. >>> dict2 = dict.fromkeys( range(2, 5), 'hello')
  5. >>> dict2
  6. {2: 'hello', 3: 'hello', 4: 'hello'}
  7. >>> dict1.update(dict2)
  8. >>> dict1
  9. {1: None, 2: 'hello', 3: 'hello', 4: 'hello'}
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-11 19:04:33 | 显示全部楼层
很详细 赞一个
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 14:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表