• Python【每日一问】36


    问:

    基础题:

    809*x=800*x+9*x+1 其中 x 代表的两位数, 8*x 的结果为两位数, 9*x 的结果为 3 位数。求 x ,及计算 809*x 的结果。

    提高题:

    对文件"命运.txt"进行字符频次统计,并将所有字符按照频次高低排序,将排序后的字符及其频次输出到文件"命运-频次排序.txt"
    字符包括中文、英文、标点等,但不包括空格和回车
    输出格式要求:
    (1)字符与频次之间采用冒号 :分隔
    (2)一个字符一行,比如
    理:224
    斯:120 
    卫:100

    答:

    基础题:

    809*x=800*x+9*x+1 其中 x 代表的两位数, 8*x 的结果为两位数, 9*x 的结果为 3 位数。求 x ,及计算 809*x 的结果。

    方法1:

    for x in range(10, 100):    
        if (10 <= 8*x < +100) and (100 <= 9*x <= 1000):        
        print(x)        
        print(809*x)

    方法2:

    a = 809
    for i in range(10, 100):    
        b = a * i + 1    
        if 1000 <= b <= 10000 and 8 * i < 100 and 9 * i > 99:        
        print(i)        
        print(b)
    
    

    提高题:

    对文件"命运.txt"进行字符频次统计,并将所有字符按照频次高低排序,将排序后的字符及其频次输出到文件"命运-频次排序.txt"
    字符包括中文、英文、标点等,但不包括空格和回车
    输出格式要求:
    (1)字符与频次之间采用冒号 :分隔
    (2)一个字符一行,比如
    理:224
    斯:120 
    卫:100


    方法1:

    txt = open('命运.txt', 'r', encoding='utf-8').read()
    txt = txt.replace('
    ', '')
    count = {}
    for word in txt:    
        count[word] = count.get(word, 0) + 1
    counts = sorted(count.items(), key=lambda x: x[1], reverse=True)
    for word, cnt in counts:    
        print(f'{word} : {cnt}')    
        file = open('命运-频次排序.txt', 'a+', encoding='utf-8')    
        file.write(f'{word} : {cnt}' + '
    ')

    方法2:

    f = open(r'命运.txt', 'r', encoding="utf-8")
    m = f.read().replace('
    ', '')
    target = {}
    for word in m:    
        target[word] = target.get(word, 0) + 1
    # print(target)
    ​
    target = sorted(target.items(), key=lambda x: x[1], reverse=True)
    ​
    with open('命运-频次排序1.txt', 'w', encoding='utf8') as output:    
    for tar, count in target:        
        output.write('{}:{}
    '.format(tar, count))
        
    f.close()

     





  • 相关阅读:
    ps4 如何导出切片 单个图片
    测试webservice的时候,如果出现这个错误:"The test form is only available for requests from the local machine"
    js jquery 按钮点击后 60秒之后才能点击 60秒倒计时
    有空研究一下 superwebsocket (底层是 supersocket) 用来实现 web聊天什么的
    Vue学习笔记一:初识Vue
    被爬虫了,嘻嘻嘻
    Mybatis-generator自动生成器
    SpringCloud笔记五:Feign
    SpringCloud笔记四:Ribbon
    SpringCloud笔记三:Eureka服务注册与发现
  • 原文地址:https://www.cnblogs.com/ElegantSmile/p/10989023.html
Copyright © 2020-2023  润新知