zoukankan      html  css  js  c++  java
  • Python编程 从入门到实践-6字典中

    笔记出处(学习UP主视频记录) https://www.bilibili.com/video/av35698354?p=12

    6.3 遍历字典

    6.3.1 遍历所有的键-值对

    user_0 = {
            'username': 'efermi',
            'first': 'emrico',
            'last': 'fermi',
            }
    for key, value in user_0.items():
        print("
    Key: " + key)
        print("Value: " + value)

    Key: username
    Value: efermi

    Key: first
    Value: emrico

    Key: last
    Value: fermi

    user_0 = {
            'username': 'efermi',
            'first': 'emrico',
            'last': 'fermi',
            }
    for k, v in user_0.items():
        print("
    Key: " + k)
        print("Value: " + v)

    Key: username
    Value: efermi

    Key: first
    Value: emrico

    Key: last
    Value: fermi

    favorite_languages = {
        'jen': 'python',
        'sarah': 'c',
        'edward': 'ruby',
        'phil': 'python',
        }
    for name, language in favorite_languages.items():
        print(name.title() + "'s favorite language is " +
            language.title() + ".")

    Jen's favorite language is Python.
    Sarah's favorite language is C.
    Edward's favorite language is Ruby.
    Phil's favorite language is Python.

    6.3.2 遍历字典中的所有键

    favorite_languages = {
        'jen': 'python',
        'sarah': 'c',
        'edward': 'ruby',
        'phil': 'python',
        }
    for name in favorite_languages.keys():
        print(name.title())

    Jen
    Sarah
    Edward
    Phil

    6.3.3 按顺序遍历字典中的所有键

    favorite_languages = {
        'jen': 'python',
        'sarah': 'c',
        'edward': 'ruby',
        'phil': 'python',
        }
    for name in sorted(favorite_languages.keys()):
        print(name.title() + ", thank you for taking the poll.")

    Edward, thank you for taking the poll.
    Jen, thank you for taking the poll.
    Phil, thank you for taking the poll.
    Sarah, thank you for taking the poll.

    6.3.4 遍历字典中的所有值

    favorite_languages = {
        'jen': 'python',
        'sarah': 'c',
        'edward': 'ruby',
        'phil': 'python',
        }
    
    print("The following languages have been mentioned:")
    for language in favorite_languages.values():
        print(language.title())

    Python
    C
    Ruby
    Python

    favorite_languages = {
        'jen': 'python',
        'sarah': 'c',
        'edward': 'ruby',
        'phil': 'python',
        }
    
    print("The following languages have been mentioned:")
    for language in set(favorite_languages.values()):
        print(language.title())

    # set可以找出列表中独一无二的元素,并使用这些元素来创建一个集合

    The following languages have been mentioned:
    C
    Python
    Ruby

    Caesar卢尚宇

    2020年3月17日

  • 相关阅读:
    命令行参数(argc, argv)
    [转]CodeSmith和PowerDesigner的使用安装和数据库创建
    INADDR_ANY的确切含义
    C++的CreateThread实例
    浅谈VC++中预编译的头文件放那里的问题分析
    [转]Windows网络编程学习-面向连接的编程方式
    char 转wchar_t 及wchar_t转char
    connect函数详解
    正斜杠(/)与反斜杠()区别
    C/C++关于string.h头文件和string类
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/12512059.html
Copyright © 2011-2022 走看看