python筛选出不合格密码的用户

有如下数据:筛选出不合格密码的用户,对出现至少四个连续数值为不合格密码,例如"1234"、"8765"为不合格密码

用户名密码
X12345678
Y87654321
O10293847
P39485726
Q28475639
R19283746
S91827364
T56473829
U83746592
V28374659
W74839201
A112093847
B190817263
C1475876510

最终结果:

用户名密码
X12345678
Y87654321
C1475876530

解决办法:
第一种解决办法:

import pandas as pd
import re

# 样本数据
data = {
    "用户名": ["X", "Y", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "A1", "B1", "C1"],
    "密码": ["12345678", "87654321", "10293847", "39485726", "28475639", "19283746", "91827364",
           "56473829", "83746592", "28374659", "74839201", "12093847", "90817263", "475876530"]
}


# 创建DataFrame
df = pd.DataFrame(data)

# 检查密码是否包含至少四个连续的递增或递减数值
def check_invalid_password(password):
    # 正则表达式匹配4个或更多的连续递增数字
    increasing_pattern = r'0123|1234|2345|3456|4567|5678|6789'
    # 正则表达式匹配4个或更多的连续递减数字
    decreasing_pattern = r'9876|8765|7654|6543|5432|4321|3210'
    
    # 检查密码是否符合递增或递减的条件
    if re.search(increasing_pattern, password) or re.search(decreasing_pattern, password):
        return True
    return False

# 过滤出不合格密码的用户
invalid_passwords = df[df['密码'].apply(check_invalid_password)]

# 输出不合格的密码
print(invalid_passwords)

第二种:

import pandas as pd
import re

# 样本数据
data = {
    "用户名": ["X", "Y", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "A1", "B1", "C1"],
    "密码": ["12345678", "87654321", "10293847", "39485726", "28475639", "19283746", "91827364",
           "56473829", "83746592", "28374659", "74839201", "12093847", "90817263", "475876540"]
}


# 创建DataFrame
df = pd.DataFrame(data)


#定义了一个名为 check_invalid_password 的函数,该函数接受一个名为 password 的参数。这个函数的作用是检查给定的 password 字符串是否包含至少四个连续的递增或递减数字。
#如果包含,则返回 True,否则返回 False。
def check_invalid_password(password):
    """
    检查密码是否包含至少四个连续的递增或递减数值。

    Args:
        password (str): 要检查的密码。

    Returns:
        bool: 如果密码包含至少四个连续的递增或递减数值,则返回 True,否则返回 False。
    """
    for i in range(len(password) - 3):
        # 检查递增序列
        if ord(password[i]) + 1 == ord(password[i+1]) == ord(password[i+2]) - 1 == ord(password[i+3]) - 2:
            return True
        # 检查递减序列
        if ord(password[i]) - 1 == ord(password[i+1]) == ord(password[i+2]) + 1 == ord(password[i+3]) + 2:
            return True
    return False

# 过滤出不合格密码的用户
invalid_passwords = df[df['密码'].apply(check_invalid_password)]

# 输出不合格的密码
print(invalid_passwords)

最终输出结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值