如何用Python的fromkeys()方法高效生成字典?

2026-06-11 07:221阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计80个文字,预计阅读时间需要1分钟。

如何用Python的fromkeys()方法高效生成字典?

功能:快速创建字典特点:常用value,seq=[google, ie, firefox]

作用:快速创建字典

如何用Python的fromkeys()方法高效生成字典?

特点:共用value

seq = [‘google‘, ‘ie‘, ‘firefox‘] # seq为可迭代对象(str, list, tuple, dict, set) dic = dict.fromkeys(seq) print(dic) # {‘google‘: None, ‘ie‘: None, ‘firefox‘: None} dic2 = dict.fromkeys(seq, 10) print(dic2) # {‘google‘: 10, ‘ie‘: 10, ‘firefox‘: 10}

本文共计80个文字,预计阅读时间需要1分钟。

如何用Python的fromkeys()方法高效生成字典?

功能:快速创建字典特点:常用value,seq=[google, ie, firefox]

作用:快速创建字典

如何用Python的fromkeys()方法高效生成字典?

特点:共用value

seq = [‘google‘, ‘ie‘, ‘firefox‘] # seq为可迭代对象(str, list, tuple, dict, set) dic = dict.fromkeys(seq) print(dic) # {‘google‘: None, ‘ie‘: None, ‘firefox‘: None} dic2 = dict.fromkeys(seq, 10) print(dic2) # {‘google‘: 10, ‘ie‘: 10, ‘firefox‘: 10}