更新**《从零开始学python数据分析与挖掘》课后习题答案**
最近在入手数据分析,有喜欢的朋友可以一起来试试呀,理论的阅读和self_coding无疑会很大程度的提高我们的能力。这是第三章课后习题的全部答案,如果有问题的话还请大家多多指正,共同完善,后续还会发布其他章节的内容
import re
#正则表达式 re.findall split sub 查找 分割 删除
string1 = '2001-08-12,2004-09-04'
split = re.findall('(\d*-\d*-\d*)', string1)
print(split)
string2 = '2是1厅 | 101.62平方米 | 顶层/3dva | 朝南\n 上海- 普通- 2005年建'
split2 = re.split('[-|\n]',string2)
print(split2)
string3 = '据悉, 这次发射的4台整齐狞獾属于国际核聚变(ITER)的和二级压力,先后完成了,占空,放入实验。'
split3 = re.sub('[,,。\s*a-zA-Z0-9()]','',string3)
print(split3)
#字典方法解析
dictionary1 = {'身高':[1,2,34],'体重':[233242,234],'姓名':'周棒'}
for a,b in dictionary1.items():
print(a,b)
#exercise 1
list1 = [1,1,2,3,5,8,13]
list1.append(21)
print(list1)
#exercise 2
list2 = ['语文','数学','英语','物理']
list2.append('化学')
list2.append('美术')
print(list2)
#exercise3
#元组是不可变的不能更改内容只能去访问,而列表可以访问可以添加可以修改,其方法内容差不多
#exercise4
dict4 = {'name':'Lucy','age':27,'gender':'female'}
print(dict4['age']) #直接索引关键字得到value
print(dict4.get('age')) #使用get方法通过关键字得到value
#exercise5
dict4.setdefault('income',10000) #通过默认的方式添加新对
dict4.update({'income1':10000}) #通过update的方式进行更新
dict4['income2'] = 10000 #通过直接添加新对的方式
#还有popitem(key)方法
#exxercise6
string6 = '18.79元/500g'
split6 = re.findall('(\d*\.\d*)',string6) #正则表达式的运用
print(split6)
#excrcise7
id = '123456199001107890'
print(re.findall('\d{16}(\d)',id))
#exercise8
id = '123456199001107890'
print(id[6:14])
#exetcise9
def relative(a = 23,b =3):
if a//b == 0:
return 1
elif str(a).find(str(b)) != -1:
return 1
else:
return -1
res = relative(23,3)
if res == 1:
print('{} and {} is relative'.format(23,3))
else:
print('{} and {} is not relative'.format(23,3))
#exercise10
ls3 = ['one','two','three','four','five','six','seven','eight','nine']
a = []
for i in range(len(ls3)):
if ls3[i].find('e') != -1:
a.append(ls3[i])
print("ls3列表中带'e'的元素有:")
print(a)
#exercise11
def sum_dengbi(n):
if n == 1:
print('等比数列的前1项之和为:3')
return 3
elif n > 1:
res = 3
b = 1
for i in range(n-1):
b = b*2
temp = res
res = temp + 3*b
print('等比数列的前{}项之和为:{}'.format(n,res))
return res
else:
print('您输入的数字为负数请重新输入')
return -1
n = int(input('请输入你所需要计算的前n项和'))
res = sum_dengbi(n)
#exercise12
#这个解法就相当于我们不知道我们的列表元素,用编程的方法计算它,或者就是列表很长肉眼看不到的全部的元素
list11 = ['A','A','B','A','C','A','B','A','C','B','C','A','C']
list_cp = list11.copy()
list_cp.sort()
ind = [list_cp[0]]
j = 0
i = 0
while(i <= len(list11)-1):
while( i <= len(list11)-1 and list_cp[i] == ind[j]): #这段的代码就是将排序之后的元素作比较查看它有多少个unique的元素
i = 1+i
if i > len(list11)-1:
break
j = j+1
ind.append(list_cp[i])
dict_12 = {}
for i in range(j+1):
dict_12[ind[i]] = list_cp.count(ind[i])
print(dict_12)
#exercise13
def load_test(usename, password,i):
Usename = 'python'
Password = 'abc123'
if Usename != usename or Password != password:
if i > 1:
print('用户名或者密码错误,你还剩{}次机会!'.format(i-1))
else:
print('用户名或者密码错误,请24小时后尝试!')
return -1
elif Usename == usename and Password == password:
print('恭喜你,登录成功!')
return 1
i = 5
while(i > 0):
use = input('请输入用户名:')
pas = input('请输入密码:')
res = load_test(use, pas, i)
if res == 1:
break
i = i-1
#exercise14 以双嵌套列表的行为进行存储
ls5 = ['1室1厅 | 44.39平方米 | 南 | 简装房 - 高楼层(共6层) | 1996年建', '1室0厅 | 29.39平方米 | 北 — 精装修 | 低楼层(共5层) | 1957年建',
'2室1厅 | 55.65平方米 | 南 | 简装 - 高楼层(共6层)| 1995年建', '3室2厅 | 118.41平方米 | 南 - 精装 | 低楼层(共39层)']
size = []
year = []
for i in range(len(ls5)):
size.append(re.findall('(\d*\.\d*)',ls5[i]))
year.append(re.findall('(\d*?)年',ls5[i]))