Python 字典(Dictionary) fromkeys()方法

Python 教程 · 2019-02-03 07:12:11

描述

Python 字典 fromkeys() 函数用于创建一个新字典,以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值。

语法

fromkeys()方法语法:

dict.fromkeys(seq[, value])

参数

  • seq -- 字典键值列表。
  • value -- 可选参数, 设置键序列(seq)的值。

返回值

该方法返回一个新字典。

实例

以下实例展示了 fromkeys()函数的使用方法:

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- seq = ('Google', 'Codercto', 'Taobao') dict = dict.fromkeys(seq) print "新字典为 : %s" % str(dict) dict = dict.fromkeys(seq, 10) print "新字典为 : %s" % str(dict)

以上实例输出结果为:

新字典为 : {'Google': None, 'Taobao': None, 'Codercto': None}
新字典为 : {'Google': 10, 'Taobao': 10, 'Codercto': 10}

点击查看所有 Python 教程 文章: https://www.codercto.com/courses/l/8.html

查看所有标签

The Intersectional Internet

The Intersectional Internet

Safiya Umoja Noble、Brendesha M. Tynes / Peter Lang Publishing / 2016

From race, sex, class, and culture, the multidisciplinary field of Internet studies needs theoretical and methodological approaches that allow us to question the organization of social relations that ......一起来看看 《The Intersectional Internet》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试