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

  • 相关阅读:
    微服务之集成(四)
    后缀数组
    HDU-6514 Monitor(二维前缀和+差分)
    前缀和、二维前缀和与差分
    2019年icpc上海网络赛 B Light bulbs (分块、差分)
    树状数组
    2019ICPC 上海网络赛 L. Digit sum(二维树状数组+区间求和)
    [USACO09MAR]向右看齐Look Up(单调栈、在线处理)
    Pay Back(模拟)
    [USACO09MAR]Moon哞哞叫Moon Mooing(模拟)
  • 原文地址:https://www.cnblogs.com/zhangyueba/p/12236305.html
Copyright © 2011-2022 走看看