zoukankan      html  css  js  c++  java
  • 杜--------前期作业以及英文歌词频统计

    作业一:Mad Libs游戏,华氏与摄氏转换

    1.创作一个让人欢乐的有趣的故事;

    2.提示伙伴输入相应类别的词;

    3.将故事完整地输出。

    A1=input('请输入一个昵称:')
    B1=input ('请输入第二个昵称:')
    print('在公交上{}刷公交卡,却被{}告知卡已作废'.format(A1,B1))

    华氏温度 与 摄氏温度的相互转换

    公式:摄氏 ℃=5/9(°F-32) 华氏°F= ℃×9/5+32

    1.输入摄氏温度

    2.转换成华氏温度

    3.输出输出结果

    celsius=input('请输入摄氏度:')
    fahrenheit=float(celsius) * 9/5 + 32
    print('摄氏度{}转为华氏度为:{}'.format(celsius,fahrenheit))

    作业二:条件循环字符串

    完成完整的温度转换程序

    while True:
        a=int(input('摄氏度转华氏度请按1,华氏度转摄氏度请按2'))
        if a==1:
            celsius=float(input('请输入摄氏度:'))
            fahrenheit=float(celsius) * 9/5 + 32
            print('摄氏度{}转为华氏度为:{}'.format(celsius,fahrenheit))
            
        elif a==2:
            fahrenheit=float(input('请输入华氏度:'))
            celsius=float((fahrenheit-32)*5/9)    
            print('{}华氏度转换成摄氏度为{}'.format(fahrenheit,celsius))
    
        else:
            break;

    猜数字游戏(猜价格,猜年龄等)

    print('猜猜我多大')
    age=18
    while True:
        
        a=int(input('输入你所猜想的年龄:'))
        if a>18:
            print('很遗憾你猜大了')
        elif a<18:
            print('猜的小了')
        elif a==18:
            print('恭喜你猜对了')
        else:
            break;
        

    解析身份证号、学号不同片段的含义

    number='442351201801014425'
    if(int(number[-2])%2==0):
        print('该身份证持有者的性别是:女')
    else:
        print('该身份证持有者的性别是:男')
        
        
    print('城市编码为:'+number[0:6] ,',出生年月为'+number[6:14])

    字符串的:连接,重复,in判断

    str='hello'
    print(str*2+'world')
    
    aa="eh" in str
    if aa:
        print('yes')
    else:
        print('no')

    用for循环产生一系列网址

    for i in range(2,10):
        print('http://gz.esf.fang.com/house/i3'+str(i)+'.html')

    用for循环遍历字符串

    a='zhongdada'
    for c in a:
        print (c)
       

     作业三:numpy数组及处理:效率对比

    问题:

    • 数列:
    • a = a1,a2,a3,·····,an
    • b = b1,b2,b3,·····,bn
    • 求:
    • c = a1^2+b1^3,a2^2+b2^3,a3^2+b3^3,·····+an^2+bn^3

    1.用列表+循环实现,并包装成函数

    2.用numpy实现,并包装成函数

    3.对比两种方法实现的效率,给定一个较大的参数n,用运行函数前后的timedelta表示。

    def Sum(n):
    a=list(range(n))
    b=list(range(0,10*n,4))
    c=[]
    for i in range(len(a)):
    c.append(a[i]**2+b[i]**3)
    return c
    print(Sum(5))

     

    
    

    import numpy as np
    def Sum2(n):
    a=np.array(range(n))
    b=np.array(range(0,10*n,n))
    s=[]
    for i in range(len(b)):
    s.append(a[i]**2+b[i]**3)
    return s
    print(Sum2(20))

     

    import datetime
    def h():
        t1=datetime.datetime.now()
        Sum(1000)
        t2=datetime.datetime.now()
        Sum2(1000)
        t3=datetime.datetime.now()
        print("sum的执行时间为:",t2-t1,"Sum2的执行时间为:",t3-t1)
    h()

    可见numpy在速度上要优先

    作业四:组合数据类型,英文词频统计

    练习:

    总结列表,元组,字典,集合的联系与区别。

    列表,元组,字典,集合的遍历。

     1.列表是可变的,可进行增删和更新,用[ ]表示

    2.元组和列表的差别在于元组不可修改,只可读,用()表示

    3.字典存储键值对数据,用key和value以及{ }定义

    4.集合内没有重复的元素,并且是无序的

    【遍历列表】

    b=['zhong','meng','yuan']
    for h in b:
        print(h)

    【遍历元组】

    a=(1,'a',3,'v','er','vv')
    for i in a :
        print (i)

    【遍历字典】

    dic={'name':'zmy','number':'2014','jiujiujiu':'xiuxiuxiu'}
    for key in dic.keys():
        print(key)
    for value in dic.values():
        print(value)
    for key,value in dic.items():
        print(key, ':', value)

    【遍历集合】

    friends={'amy','tom','gery','vicky'}
    for i in friends:
        print(i)

    英文词频统计:

    • 下载一首英文的歌词或文章str
    • 分隔出一个一个的单词 list
    • 统计每个单词出现的次数 dict

     

    将所有大写转换为小写
    将其中所有的标点符号’ : ” , . ?!和特殊符号替换为空格 str.replace()
    去除两端的空格 str.strip()
    统计一个单词出现的次数 str.count()
    分隔出一个一个的单词 str.split()


    song=''''every night in my dreams 
    i see you, i feel you,
    that is how i know you go on 
    far across the distance 
    and spaces between us 
    you have come to show you go on 
    near, far, wherever you are 
    i believe that the heart does go on 
    once more you open the door 
    and you're here in my heart 
    and my heart will go on and on 
    love can touch us one time 
    and last for a lifetime 
    and never let go till we're one 
    love was when i loved you 
    one true time i hold to 
    in my life we'll always go on 
    near, far, wherever you are 
    i believe that the heart does go on 
    once more you open the door 
    and you're here in my heart 
    and my heart will go on and on 
    there is some love that will not go away 
    you're here, there's nothing i fear,
    and i know that my heart will go on 
    we'll stay forever this way 
    you are safe in my heart 
    and my heart will go on and on'''
    
    
    
    a=''',.!'''
    for i in a:
        song=song.replace('i ',' ')
    song=song.replace('
    ',' ')
    song=song.split(' ')
    
    
    dic={}
    b=set(song)
    for i in b:
        dic[i]=song.count(i)
    print(dic)





      

  • 相关阅读:
    python学习笔记 day28 内置函数进阶实例
    python学习笔记 day28 面向对象的进阶
    python学习笔记 day27 内置方法
    python学习笔记 day27 反射(二)
    python学习笔记 day26 反射-----hasattr getattr delattr
    python学习笔记 day26 类方法classmethod 和 静态方法 staticmethod ---------都是面向对象的内置函数(装饰器)
    python学习笔记 day26 私有属性 和 property
    python学习笔记 day25 封装
    python学习笔记 day25 多态
    #Leetcode# 728. Self Dividing Numbers
  • 原文地址:https://www.cnblogs.com/ZHONGmy/p/9752798.html
Copyright © 2011-2022 走看看