一、概览
二、参考代码
1.查找数字 #2663
from collections import Counter
a = list(input().split())
s = set(a)
b = [x for x in s if a.count(x)%2==1]
print(b[0])
# n = Counter(a)
# m = [x for x in Counter(a) if n[x]%2==1]
# print(m[0])
2.共有前缀(列表/集合)#74033
def f(strs):
#定义函数,实现题目要求
s=''
# 前缀长度上限
x = strs[0]
# 将各单词的字母逐个放入集合中
# 如果集合的len==1,则相同
# 继续放下一个字母
for i in range(len(x)):
# 集合推导式简化代码
set1 = set([c[i] for c in strs])
if len(set1)==1:
s+=x[i]
else:
break
if s=='':
return 'NOT FOUND'
return s
strs=list(input().split())
print(f(strs))
3.查找特征数(集合/列表)#74432
a = input()
ls = list(a.split())
ls = list(map(int,ls))
# 首先统计有哪些数据————集合
# 自动剔除重复元素,比遍历快多了
set1 = set(ls)
lss = [] # 存放特征数
# 统计set中的数在list中出现的次数————count()
lss = [x for x in set1 if ls.count(x)==x]
# for i in set1:
# if ls.count(i) ==i:
# lss.append(i)
if len(lss)>0:
#lss.sort(reverse = True)
print(max(lss))
else:
print('-1')
4.查找数列中重复数字 # 74483
###使用字典效率也比ls.count()高
###Counter返回的也是字典,二者的底层应该差不多
from collections import Counter
###ls.count()复杂度,n^2
###字典复杂度,n
a = list(input().split())
s1 = set(a) # 这个不太耗时间
# 使用 Counter 统计数字出现的次数,大于1则添加到结果列表
counter = Counter(a)
# 返回的是一个字典
#(key,value)对应(元素,出现次数)
# 速度比list.count()快!!
ls = [int(x) for x in counter if counter[x] > 1]
ls.sort()
print(ls)
5.个人数据合并 #79920
import random
s = int(input())
random.seed(s)
p = int(input())
a = []
b = []
for i in range(5):
a.append([i+1,random.randint(0,100),random.randint(0,100)])
b.append([1,random.randint(0,100)])
for i in range(5):
a[i].insert(p,b[i][1])
print(a)
6.集合添加元素 # 75194
a = int(input())
s = set()
for i in range(a):
s.add(input())
print(len(s))
7. 通讯录(删除)#2433
dic={'张自强': ['12652141777', '材料'], '庚同硕': ['14388240417', '自动化'], '王岩': ['11277291473', '文法']}
def menu():
print('''\n欢迎使用PYTHON学生通讯录
1:添加学生
2:删除学生
3:修改学生信息
4:搜索学生
5:显示全部学生信息
6:退出并保存''')
a = input()
print(dic)
menu()
if a!='2':
print('ERROR')
else:
b = input()
if b in dic.keys():
##############按“键”删除
del dic[b]
print('Success')
print(dic)
else:
print('No Record')
print(dic)
8.通讯录(查询)#2435
dic={'张自强': ['12652141777', '材料'], '庚同硕': ['14388240417', '自动化'], '王岩': ['11277291473', '文法']}
def menu():
print('''\n欢迎使用PYTHON学生通讯录
1:添加学生
2:删除学生
3:修改学生信息
4:搜索学生
5:显示全部学生信息
6:退出并保存''')
a = input()
print(dic)
menu()
if a!='4':
print('ERROR')
else:
b = input()
if b in dic.keys():
##############按“键”删除
print(" ".join([b,dic[b][0],dic[b][1]]))
print('Success')
print(dic)
else:
print('No Record')
print(dic)