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
  • 相关阅读:
    SQL Server: TRUSTWORTHY数据库属性
    查看SQL Server的版本
    SQL学习笔记7
    SQL Server Database:ReadOnly
    Shell编程(五)find与grep命令简介及正则表达式
    windowXP 任务计划无法启动 错误代码:0X80041315
    ORA14400: inserted partition key does not map to any partition
    Windows 批处理实现 定时打开IE 延时一段时间后 关闭IE
    尝试创建自定义事件日志时,将会收到“Requested registry access is not allowed(不允许所请求的注册表访问权)”错误消息
    Linux的用户和组群管理
  • 原文地址:https://www.cnblogs.com/zhaop8078/p/11811397.html
Copyright © 2011-2022 走看看