zoukankan      html  css  js  c++  java
  • day 08 作业

    '''1. 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],
    将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中
    
    即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}'''
    
    i=[11,22,33,44,55,66,77,88,99]
    k1=[]
    k2=[]
    for l in i :
        if l>66:
            k1.append(l)
        elif l<66 :
            k2.append(l)
    lis={'k1':k1,'k2':k2}
    print(lis)
    
    

    2.223

    # 2. 统计s='hello alex alex say hello sb sb'中每个单词的个数
    #
    # 结果如:{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}
    
    s='hello alex alex say hello sb sb'
    hello=s.count('hello')
    alex=s.count('alex')
    say=s.count('say')
    sb=s.count('sb')
    print({'hello':hello,'alex':alex,'say':say,'sb':sb})
    
    
    # 2. 统计s='hello alex alex say hello sb sb'中每个单词的个数
    #
    # 结果如:{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}
    
    s='hello alex alex say hello sb sb'
    hello=s.count('hello')
    alex=s.count('alex')
    say=s.count('say')
    sb=s.count('sb')
    print({'hello':hello,'alex':alex,'say':say,'sb':sb})
    
    
    # 2. 统计s='hello alex alex say hello sb sb'中每个单词的个数
    #
    # 结果如:{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}
    
    s='hello alex alex say hello sb sb'
    hello=s.count('hello')
    alex=s.count('alex')
    say=s.count('say')
    sb=s.count('sb')
    print({'hello':hello,'alex':alex,'say':say,'sb':sb})
    
    
    . 假设有一个文件test.txt,内有如下内容
    # l=[
    #     {'name':'alex','age':84},
    #     {'name':'oldboy','age':73},
    #     {'name':'egon','age':18},
    # ]
    # 需求:
    # 1. 读取文件内容
    # 2. 计算这三个人的年龄总和
    with open(
        r'D:pycharm项目练习第一周作业题	est.txt')as f:
        data=f.read()
        # print(data)
    l = [{'name': 'alex', 'age': 84}, {'name': 'oldboy', 'age': 73}, {'name': 'egon', 'age': 18} ]
    alex_age=l[0]['age']
    oldboy_age=l[1]['age']
    egon_age=l[2]['age']
    sum_age=alex_age+oldboy_age+egon_age
    print(sum_age)
    
  • 相关阅读:
    聚类算法优秀博客链接
    读安晓辉老师的访谈有感
    机器学习概览之常见算法汇总
    稀疏表达和压缩感知的一些对比
    [Linked List]Reverse Linked List,Reverse Linked List II
    [Linked List]Palindrome Linked List
    [Linked List]Remove Nth Node From End of List
    [Tree]Binary Tree Inorder Traversal
    [Tree]Binary Tree Preorder Traversal
    [stack]Evaluate Reverse Polish Notation
  • 原文地址:https://www.cnblogs.com/fwzzz/p/11448760.html
Copyright © 2011-2022 走看看