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\项目\练习\第一周作业题\test.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)
posted @ 2019-09-02 20:24  fwzzz  阅读(103)  评论(0编辑  收藏  举报