zoukankan      html  css  js  c++  java
  • Python 字典(Dictionary) fromkeys()方法

    描述

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

    语法

    fromkeys()方法语法:

    dict.fromkeys(seq[, value])

    参数

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

    返回值

    该方法返回一个新字典。

    实例

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

    实例(Python 2.0+)

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

    以上实例输出结果为:

    新字典为 : {'Google': None, 'Taobao': None, 'Runoob': None}
    新字典为 : {'Google': 10, 'Taobao': 10, 'Runoob': 10}
  • 相关阅读:
    HTML和CSS 基本要点必看
    CSS
    六个选择器
    HTML初级课程 (自学可懂)
    this
    1.作用域链
    js if 语句
    js数据类型
    ifelse语句
    三元运算符
  • 原文地址:https://www.cnblogs.com/yc10086/p/13345389.html
Copyright © 2011-2022 走看看