一、目的
掌握集合与字典的定义及其操作使用方法;
二、内容
1. 输入一个整数列表L,判断L中是否存在相同的数字:
(1)若存在,输出YES,否则输出NO;
代码:
1 L=input('请输入一列整数,并以空格隔开:') 2 lis=L.split(' ') 3 if len(set(lis))!=len(lis): 4 print('YES') 5 else: 6 print('NO')
运行结果:
(2)若存在,输出YES,同时输出相同的数字;否则输出NO。
代码:
1 L=input('请输入一列整数,并以空格隔开:') 2 lis=L.split(' ') 3 if len(set(lis))!=len(lis): 4 print('YES') 5 for i in range(len(lis)-1): 6 for j in range(i+1,len(lis)): 7 if lis[i]==lis[j]: 8 print(lis[i]) 9 else: 10 print('NO')
运行结果:
2. 从英文字母(区分大小写)和 0~9数字组成的列表中随机生成10个6位数的密码。
代码:
1 from random import sample 2 s=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u", 3 "v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P", 4 "Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"] 5 for i in range(10): 6 answer = sample(s,6) 7 for j in answer: 8 print(j,end='') 9 print(' ')
运行结果: