zoukankan      html  css  js  c++  java
  • Python 字典 dic.items() 方法

    描述

    Python 字典 items() 方法以列表返回可遍历的(键, 值) 元组数组。

    语法

    items()方法语法:

    dict.items()

    参数

    • NA。

    返回值

    返回可遍历的(键, 值) 元组数组。

    实例

    以下实例展示了 items() 方法的使用方法:

    实例

    1
    2
    3
    4
    5
    #!/usr/bin/python3
      
    dict = {'Name''Runoob''Age'7}
      
    print ("Value : %s" %  dict.items())

    以上实例输出结果为:

    1
    Value : dict_items([('Age'7), ('Name''Runoob')])

    2 篇笔记

    遍历例子:

    1
    2
    3
    dict = {'Name''Runoob''Age'7}
    for i,j in dict.items():
        print(i, ": ", j)

    输出:

    1
    2
    Name :   Runoob
    Age :    7

    将字典的 key 和 value 组成一个新的列表:

    1
    2
    3
    4
    5
    6
    7
    d={1:"a",2:"b",3:"c"}
    result=[]
    for k,v in d.items():
        result.append(k)
        result.append(v)
     
    print(result)

    输出:

    1
    [1'a'2'b'3'c']
  • 相关阅读:
    Count on a tree
    图论1 1009
    DP2 1008
    DP1 1008
    NOIP 模拟 1006
    2019 CSP-S 初赛退役记
    9.13——TEST NOIP模拟测试
    [洛谷P2387][NOI2014]魔法森林
    [洛谷P2596][ZJOI2006]书架
    [BZOJ4241]历史研究
  • 原文地址:https://www.cnblogs.com/tonyxiao/p/14348575.html
Copyright © 2011-2022 走看看