• 习题练习2


    习题1

      将字符串"python"转化为列表(记为lst),然后完成如下操作:

      - 将字符串"rust"中的每个字母作为独立元素追加到lst中

      - 对lst排序

      - 删除lst中的重复元素

    >>> s = 'python'
    >>> lst = list(s)
    >>> lst
    ['p', 'y', 't', 'h', 'o', 'n']
    >>> r = 'rust'
    >>> lst.extend(r)        #追加字符串
    >>> lst
    ['p', 'y', 't', 'h', 'o', 'n', 'r', 'u', 's', 't']
    >>> lst.sort()        #排序
    >>> lst
    ['h', 'n', 'o', 'p', 'r', 's', 't', 't', 'u', 'y']
    >>> set(lst)        #去重
    {'s', 't', 'y', 'r', 'n', 'o', 'p', 'u', 'h'}
    >>> list(set(lst))        #去重后转换为列表
    ['s', 't', 'y', 'r', 'n', 'o', 'p', 'u', 'h']

    习题二

      编写程序,实现如下功能:

      - 用户输入国家名称

      - 打印出所输入国家名称及其首都

    >>> nations = {'China':'Beijing', 'Japan':'Tokyo', 'India':'New Delhi', 'Sweden': 'Stockholm', 'Russian':'Moscow', 'Germany':'Berlin', 'UK':'London', 'French':'Paris', 'Swiss':'Bern', 'Egypt':'Cairo', 'Australia':'Canberra', 'New Zealand':'Wellington', 'Canada':'Ottawa', 'USA':'Washington', 'Cuba':'Havana', 'Brazil':'Brasilia'}

     >>> name = input('input a name of country:')
     input a name of country:China
     >>> capital = nations.get(name)
     >>> print("the country:", name)
     the country: China
     >>> print("the capital:", capital)
     the capital: Beijing

    习题三

      有如下技术栈名称集合: skills={'Python', 'R', 'SQL', 'Git', 'Tableau', 'SAS'},假设自己的技术是:mySkills={'Python', 'R'}

      - 判断自己所掌握的技术是否在上述技术栈范围之内

    >>> skills={'Python', 'R', 'SQL', 'Git', 'Tableau', 'SAS'}
    >>> mySkills={'Python', 'R'}
    >>> r = mySkills.intersection(skills)
    >>> bool(r)
    True
    >>> r
    {'Python', 'R'}

    习题四

      找出以下两个字典共有的键:

      {'a':1, 'b':2, 'c':3, 'd':4}

      {'b':22, 'd':44, 'e':55, 'f':77}

     >>> d1 = {'a':1, 'b':2, 'c':3, 'd':4}
     >>> d2 = {'b':22, 'd':44, 'e':55, 'f':77}

    >>> d1.keys() & d2.keys()
    {'b', 'd'}

    习题五

      字符串:songs='You raise my up so can stand on mountains You raise my up to walk on stormy seas am strong when am on your shoulders You raise me up to more than can be'

      - 制作上述字符串的单词表

      - 统计每个单词的出现次数

    >>> songs='You raise my up so can stand on mountains You raise my up to walk on stormy seas am strong when am on your shoulders You raise me up to more than can be'
    >>> songs_set = set(songs.split())
    >>> songs_set
    {'stand', 'me', 'am', 'walk', 'so', 'my', 'your', 'raise', 'stormy', 'on', 'more', 'than', 'You', 'mountains', 'up', 'be', 'when', 'seas', 'strong', 'can', 'shoulders', 'to'}
    >>> songs.count('stand')
    1
  • 相关阅读:
    【143】360云盘资源
    【142】阿蛮歌霸使用技巧
    [置顶] 程序员必知(三):一分钟知道URI编码(encodeURI)
    [置顶] Oracle job procedure 存储过程定时任务
    浅析动态表单
    DoctorNote医生处方笔记开发记录
    Step2:配置Oracle Dataguard
    IOS开发UIImage中stretchableImageWithLeftCapWidth方法的解释
    解析客户端脚本、服务器端脚本
    tomcat目录结构
  • 原文地址:https://www.cnblogs.com/zhaop8078/p/11811397.html
Copyright © 2020-2023  润新知