python通用编程二阶段:模块对象

一、薪资文件
内容如下,标题为:姓名,性别,年纪,薪资

albert male 18 3000
james male 38 30000
林玲 female 28 20000
结衣 female 28 10000

要求从文件中取出每一条记录放入列表中,列表中的每个元素都是如下格式:

{'name':'albert','sex':'male','age:18,'salary':3000}
f = open('info.txt', 'r')
list_info = []
for line in f:
    data_info = line.split(' ')
    name = data_info[0]
    sex = data_info[1]
    age = data_info[2]
    salary = data_info[3]
    people = {'name': name, 'sex': sex, 'age': int(age), 'salary': int(salary)}
    list_info.append(people)
f.close()

print(list_info)

二、根据题一,求薪资最高

# 方式一 :手写算法
def get_the_richest(list1):
    richest = list1[0]
    for i in range(len(list1)):
        if list1[i]['salary'] > richest['salary']:
            richest = list1[i]
    return richest

# 方式二:使用内置函数max与lambda组合
print('内置函数', max(list_info, key=lambda x: x['salary']))

三、将名字首字母改为大写

def change_capitalize(list1):
    for i in range(len(list1)):
        if 'a' <= list1[i]['name'][0] <= 'z':
            list1[i]['name'] = list1[i]['name'].capitalize()
    return list1


res1 = change_capitalize(list_info)
print(res1)

# need to create redundant joint
res2 = map(lambda x: x['name'].capitalize(), list_info)
print(list(res2))

四、过滤掉以a开头的名字信息

def filter_a(list1):
    list2 = []
    for i in range(len(list1)):
        if list1[i]['name'][0] != 'a':
            list2.append(list1[i])
    return list2

print(list(filter(lambda x: not x['name'].startswith('a'), list_info)))

五、用递归打印斐波那契数列

def Fibonacci(n):
    if n < 1:
        return
    elif n == 1:
        return 1
    elif n == 2:
        return 1
    else:
        return Fibonacci(n - 1) + Fibonacci(n - 2)


for i in range(1, 20):
    print(Fibonacci(i))

六、使用random模块,随机生成8位验证码,随机大小写字母和数字

import random
def get_verification():
    verification_code = ''
    for i in range(8):
        alpha1 = chr(random.randint(65, 90))
        alpha2 = chr(random.randint(97, 122))
        number = random.randint(0, 9)
        verification_code += random.choice([alpha1, alpha2, str(number)])
    return verification_code


print((get_verification()))

七、写一个模拟撞库的程序,假如密码都是用md5加密的,撞库就是用多个猜测的密码尝试比对正确的密码,比对过程一定是用md5来进行的。

import random
import hashlib


def get_password_violently():
    while True:
        
        password = ''

        # imagine password is formed by 4 numbers or alphas
        for i in range(4):
            random_alpha = chr(random.randint(65, 90))
            random_number = random.randint(0, 9)
            password += random.choice([random_alpha, str(random_number)])

        # md5 processing...
        m = hashlib.md5()
        m.update(password.encode('utf-8'))
        md5_password = m.hexdigest()

        all_alpha_number = list(map(lambda x: chr(x), range(48, 122)))

        # simulate user input password
        for a in all_alpha_number:
            for b in all_alpha_number:
                for c in all_alpha_number:
                    for d in all_alpha_number:
                        input_password = a + b + c + d

                        m = hashlib.md5()
                        m.update(input_password.encode('utf-8'))
                        input_md5_password = m.hexdigest()

                        if input_md5_password == md5_password:
                            return password


res = get_password_violently()
print("user's password", res)

八、使用re模块写一个验证手机号码是否有效的程序
首先准备一个通讯录文件,内容为:

姓名 地区 身高 体重 电话
况咏蜜 北京 171 48 11151054608
王欣妍 上海 169 46 137234523

import re


def get_model_phone(file):
    invalid_data = {}
    with open(file, 'r', encoding='utf-8') as f:
        for line in f:
            model_data_list = line.split()
            name = model_data_list[0]
            phone = model_data_list[-1]
            valid_phone = re.findall('13\d\d\d\d\d\d\d\d\d', phone)
            if valid_phone:
                invalid_data[name] = valid_phone[0]
        return invalid_data


res = get_model_phone('info2.txt')
print(res)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值