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

    6-1 人 : 使用一个字典来存储一个熟人的信息, 包括名、 姓、 年龄和居住的城市。

        该字典应包含键first_name 、 last_name 、 age 和city 。 将存储在该字典中的每项信息都打印出来。

    6-2 喜欢的数字 : 使用一个字典来存储一些人喜欢的数字。 请想出5个人的名字, 并将这些名字用作字典中的键;

            想出每个人喜欢的一个数字, 并将这些数字作为值存
            储在字典中。 打印每个人的名字和喜欢的数字。 为让这个程序更有趣, 通过询问朋友确保数据是真实的。
    6-3 词汇表 : Python字典可用于模拟现实生活中的字典, 但为避免混淆, 我们将后者称为词汇表。
          想出你在前面学过的5个编程词汇, 将它们用作词汇表中的键, 并将它们的含义作为值存储在词汇表中。
          以整洁的方式打印每个词汇及其含义。 为此, 你可以先打印词汇, 在它后面加上一个冒号, 再打印词汇的含义; 也可在一行打印词汇,

                         再使用换行符( ) 插 入一个空行, 然后在下一行以缩进的方式打印词汇的含义。

    6-1    

    friend = {'first_name':'wu',
                'last_name':'xiao','age':'18','city':'shihunjie'}
    print("my friend's name is " + friend['last_name'] + " " 
        + friend['first_name'] + " ,and his age is " + friend['age'] 
        + ",he is from " + friend['city'] + ".")

    输出:
    my friend's name is xiao wu ,and his age is 18,he is from shihunjie.

    6-2     items() 函数作用:以列表返回可遍历的(键, 值) 元组数组  

    names = {'a':1,'b':2,'c':3,'d':4,'e':5}
    for key,number in names.items():
        print(key + "'s favorite number is " + str(number))

     6-3 我他妈气死这么两行 items写成iteams查了半天真是个憨批

    names = {'del':'shanchu','print':'shuchu'}
    for name,act in names.items():
            print(name + " : ")
            print(act)

    输出:

    del :
    shanchu
    print :
    shuchu

     6-4 使用keys()和values()方法,懒得改了

    6-5

    river_country = {'nile':'Egypt','huanghe':'China','duonaohe':'Europe'}
    for name,country in river_country.items():
        print("The " + name +"runs through " + country + "." )
    for name in river_country.keys():
        print(name)
    for country in river_country.values():
        print(country)
    
    输出:
    The nileruns through Egypt.
    The huangheruns through China.
    The duonaoheruns through Europe.
    nile
    huanghe
    duonaohe
    Egypt
    China
    Europe

     6-6 调查 

    names = {'a':1,'b':2,'c':3,'d':4,'e':5}
    man = ['a','c','e','f']
    for name in names.keys():
        if name in man:
            print(name +", Thanks for accpet the survey.")
        elif name not in man:
            print(name+", We invite you to accpet the survey.")
    输出
    a, Thanks for accpet the survey.
    b, We invite you to accpet the survey.
    c, Thanks for accpet the survey.
    d, We invite you to accpet the survey.
    e, Thanks for accpet the survey.

     6-7人:

    a = {'first_name ':'六花','last_name':'小鸟游','age':'16','city':'中二病'}
    b = {'first_name ':'一护','last_name':'黑崎','age':'18','city':'死神'}
    c = {'first_name ':'大河','last_name':'逢坂','age':'20','city':'龙与虎'}
    people = [a,b,c]
    for peoplee in people:
        print(peoplee)
    输出:
    {'first_name ': '六花', 'last_name': '小鸟游', 'age': '16', 'city': '中二病'}
    {'first_name ': '一护', 'last_name': '黑崎', 'age': '18', 'city': '死神'}
    {'first_name ': '大河', 'last_name': '逢坂', 'age': '20', 'city': '龙与虎'}

     6-8 后面就是字典中嵌套字典zzzz懒得写了睡觉了明天再努力吧zzz

  • 相关阅读:
    浅析影响一个网站的因素
    23种常用设计模式的UML类图
    各版本IE兼容问题,IE6,IE7,IE8,IE9,IE10,IE11
    47种常见的浏览器兼容性问题大汇总
    5个Sublime Text 的插件推荐
    网页设计师神器,快速生成网站配色、字型等风格的工具——Stylify Me
    免费的高分辨率图库——re:splashed 可用做网页背景、设计或桌面壁纸
    MySQL(18):Select- subquery子查询
    MySQL(17):Select-union(联合查询)使用注意事项
    MySQL(16):Select-union(联合查询)
  • 原文地址:https://www.cnblogs.com/zhangyueba/p/12236305.html
Copyright © 2011-2022 走看看