2018.9.10学习内置函数、切片、字符串拼接等

今日练习:

# 题目1:从网络上使用爬虫程序得到下面字符串:
# zhangsan-18-175-70-python5;lisi-20-170-80-python5;wangwu-28-165-60-python3;zhaoliu-18-175-70-python5;xiaoming-20-171-73-python3;dahuang-21-177-73-python3;xiaomei-23-163-50-python5;runtu-24-179-70-python3
str = 'zhangsan-18-175-70-python5;lisi-20-170-80-python5;wangwu-28-165-60-python3;zhaoliu-18-175-70-python5;xiaoming-20-171-73-python3;dahuang-21-177-73-python3;xiaomei-23-163-50-python5;runtu-24-179-70-python3'
# 求:
# a、上面字符串描述了多少人的信息
infor = {}
list = []
str_new = str.split(';')
# print('这里有%s个人'%len(str_new))
# print(len(str_new))
# b、将上诉信息使用字典的数据类型进行存储
for listItem in str_new:
    innerList = listItem.split('-')
    lenght = len(innerList)
    i = 0
    for item in innerList:
        if (i == 0):
            infor['name'] = item;
        elif (i == 1):
            infor['age'] = item;
        elif (i == 2):
            infor['height'] = item;
        elif (i == 3):
            infor['weight'] = item;
        elif (i == 4):
            infor['learn'] = item;
        i += 1
    list.append(infor)
    infor = {}
print(list)
# c、得到所有人的平均年龄
allAge = 0;
for item in list:
    allAge += int(item['age'])
print(allAge / len(list))
countA = 0;
countB = 0;
# d、求每个班级的人员总数

print(len(list))

# e、求python3期的平均身高
h = 0
count = 0
for a in list:
    if (a['learn'] == 'python3'):
        h += int(a['height'])
        print(h)
        count+=1
print(h/count)
print(h)
print(count)
#
# 题目2:有一个字符串helloworld
# 使用多种方式 判断是否出现u字符,如果出现,打印找到信息,否则,输出没有找到(4种方式 )
k =0
str = 'helloworld'
#方式1
# for x in str:
#     if(x=='u'):
#         k==1
#         print('这里有')
# if(k==0):
#     print('没有')
#方式2
# if(str.count('u')==0):
#     print('没有')
# else:
#     print('有')
#方式3
# temp = str.split('u')
# if(len(temp)==1):
#     print('没有')
# else:
#     print('有了')
#方式4
# if(str.find('u'))==-1:
#     print('没有')
# else:
#     print('有了')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值