生成字典

生成字典

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

#将数字补位至足够位数的字符串
def intadd(num,n):
    diff = n - len(str(num))
    if diff == 0:
        return str(num)
    elif diff > 0:
        str1 = str(num)
        for i in range(diff):
            str1 = "0" + str1
        return str1

#将所有单个字符放到一个列表中
def singlezifu():
    list=[]
    for i in range(33,127):
        list.append(chr(i))
    return list



#两个数字字符串组合,可以单独设置每个字符串的长度
def zuhe():
    with open('txt',mode='w') as f:
        for i in range(100):
            for j in range(100):
                f.write(intadd(i,2) + intadd(j,2) + '\n')


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


#生成指定位数的密码字典
def main():
    x = int(input("你想测试几位数的密码:"))
    time_start = time.time()				#计时开始
    list0 = singlezifu()                      #单字符列表
    list1 = list(product(list0, repeat=x))      #对单字符排列组合
    list2 = TupToStr(list1)               #列表元祖转字符串

	#将列表的内容写到文件
     with open('txt3', mode='a' ) as f:
         for i in list2:
             f.write(i + '\n')
     time_end = time.time()

if __name__ == "__main__":
    main()
    print(time_end - time_start)

猜你喜欢

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