zoukankan      html  css  js  c++  java
  • 习题练习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
  • 相关阅读:
    Selenium3+python3自动化(十六)--单选框和复选框(radiobox、checkbox)
    Selenium3+python3自动化(十五)--JS处理滚动条,window.scrollTo(xpos,ypos)
    Selenium3+python3自动化(十四)--alertsconfirmprompt
    Selenium3+python3自动化(十三)--select下拉框
    Selenium3+python3自动化(十二)--iframe
    UPC-5627 Boxes(思维)
    UPC-5007 High Score(计算)
    UPC-5003 Dunglish(模拟)
    Hrbust-1830 第一个重复出现的数(线段树区间最值查询)
    UPC-5599 Minimum Sum(线段树求区间最小值+二分区间计数)
  • 原文地址:https://www.cnblogs.com/zhaop8078/p/11811397.html
Copyright © 2011-2022 走看看