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日

  • 相关阅读:
    对“一道有趣的题目”的解答
    在Debian中使用Notify
    Debian服务器安装详细流程
    Lighttpd1.4.20源码分析之插件系统(1)plugin结构体和插件接口
    Lighttpd1.4.20源码分析之etag.c(h) HTTP/1.1中的Etag域
    Lighttpd1.4.20源码分析之bitset.c(h) 位集合的使用
    伸展树
    GCC中的弱符号与强符号
    Lighttpd1.4.20源码分析之fdevent系统(3) 使用
    Lighttpd1.4.20源码分析之插件系统(3)PLUGIN_TO_SLOT宏
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/12512059.html
Copyright © 2011-2022 走看看