1. 效果图
10行随机密码,首字母不同,效果图如下:
田字格随机字符串如下:
2. 源码
# 生成随机密码
import random
import numpy as np
# 1. 生成随机密码,密码首字母不同
np.random.seed(0x1010)
str = 'abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ'
for i in range(10):
random_str = ''.join(random.sample(str, 10))
print(random_str)
# 要求密码首字母不同,去掉首字母再去生成下一个密码
str = str.replace(random_str.__getitem__(0), '')
# 2. 生成田字格随机密码,并统计各字符出现次数
str= 'abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ_0123456789'
def tianziGe(n):
list = []
for i in range(2 * n + 2):
if (i == 0 or i == n or i == 2 * n + 1):
random_str = ''.join(random.sample(str, 2 * n + 3))
list.append(random_str)
# print(random_str)
else:
random_str = ''.join(random.sample(str, 3))
random_str = random_str.__getitem__(0) + ''.join([' ' for i in range(n)]) + random_str.__getitem__(
1) + ''.join(
[' ' for i in range(n)]) + random_str.__getitem__(2)
list.append(random_str)
# print(random_str)
return list
n = eval(input())
list = tianziGe(n)
for i in list:
print(i)
dict = {}
for i in list:
for j in i.strip():
if (dict.__contains__(j)):
dict[j] += 1
else:
dict[j] = 0
print(dict)