菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python 字典(Dictionary) items()方法

Python 字典 Python 字典


描述

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

语法

items()方法语法:

dict.items()

参数

  • NA。

返回值

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

实例

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

实例(Python 2.0+)

#!/usr/bin/python# coding=utf-8tinydict = {'Google': 'www.google.com', 'Runoob': 'www.runoob.com', 'taobao': 'www.taobao.com'} print"字典值 : %s" % tinydict.items()# 遍历字典列表forkey,valuesintinydict.items(): printkey,values

以上实例输出结果为:

字典值 : [('Google', 'www.google.com'), ('taobao', 'www.taobao.com'), ('Runoob', 'www.runoob.com')]
Google www.google.com
taobao www.taobao.com
Runoob www.runoob.com

Python 字典 Python 字典

AI 思考中...

1 篇笔记 写笔记

  1. #0

    items() 方法的遍历:items() 方法把字典中每对 key 和 value 组成一个元组,并把这些元组放在列表中返回。

    d = {'one': 1, 'two': 2, 'three': 3}
    >>> d.items()
    dict_items([('one', 1), ('two', 2), ('three', 3)])
    >>> type(d.items())
    <class 'dict_items'>
    >>> for key,value in d.items():#当两个参数时
     print(key + ':' + str(value))
    one:1
    two:2
    three:3
    >>> for i in d.items():#当参数只有一个时
     print(i)
    ('one', 1)
    ('two', 2)
    ('three', 3)
    7年前 (2019年11月02日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /