2021-09-21

本文提供了《从零开始学python数据分析与挖掘》一书第三章课后习题的答案,涵盖正则表达式操作、列表与字典管理等内容,并鼓励读者一起参与学习与交流。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

更新**《从零开始学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]))
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值