1. 程式人生 > >2018.9.10學習內建函式、切片、字串拼接等

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('有了')