生成任意字符组合的密码

import time
from itertools import product
# #列表排列组合
# k = [1, 2, 3]
# print(list(product(k, k)))
# print(list(product(k, repeat=3)))



#将元祖数据转变为字符串,返回新列表
def TupToStr(list):
    list3 = []
    n = ''
    for i in range(len(list)):
        m = list[i]
        n += m
    list3.append(n)
    return list3


#生成指定位数的密码字典
def main():
    x = int(input("你想测试几位数的密码:"))

    time_start = time.time()
    with open('txt_1', mode='w' ) as f:
        for item in product([chr(i) for i in range(33,127)], repeat=x):
            list1 = TupToStr(item)
            print(list1[0])
            f.write( list1[0]+ '\n')
    time_end = time.time()
    print(time_end - time_start)


if __name__ == "__main__":
    main()

结果:

C:\Users\ZETTAKIT\PycharmProjects\chenfan\venv\Scripts\python.exe C:/Users/ZETTAKIT/PycharmProjects/chenfan/day0418/18.3.py
你想测试几位数的密码:4
205.23994374275208

Process finished with exit code 0

……

生成的文件部分展示:

!!!!
!!!"
!!!#
!!!$
!!!%
!!!&
!!!'
!!!(
!!!)
!!!*
!!!+
!!!,
!!!-
!!!.
!!!/

猜你喜欢

转载自blog.csdn.net/qq_32371827/article/details/89519028