【python】习题第7周(上)https://developer.aliyun.com/article/1507478?spm=a2c6h.13148508.setting.19.1b484f0eD2AqhJ
7-11 电话聊天狂人
输入样例:
4 13005711862 13588625832 13505711862 13088625832 13588625832 18087925832 15005713862 13588625832
输出样例:
13588625832 3
代码:
dict1 = {} a = [] n = int(input()) for i in range(n): line = input().split() if line[0] not in dict1: dict1[line[0]] = 1 elif line[0] in dict1: dict1[line[0]] += 1 if line[1] not in dict1: dict1[line[1]] = 1 elif line[1] in dict1: dict1[line[1]] += 1 m = max(dict1.values()) for key, value in dict1.items(): if value == m: a.append(key) if len(a) == 1: print(min(a), m) else: print(min(a), m, len(a))
对比别人写的代码:
n=int(input()) call={} while n: lst=list(map(int,input().split())) n-=1 for i in lst: if i not in call: call[i]=1 else: call[i]+=1 cnt,frq=1,0 rst=[] for k in sorted(call.items(),key=lambda call:call[1],reverse=True): if frq==0: min_num=k[0] frq=k[1] if k[1]==frq: rst.append(k) if min_num>k[0]: min_num=k[0] else: break if len(rst)==1: print(min_num,frq) else: print(min_num,frq,len(rst)) # sample小数据,狂人唯一 # 小数据,多个狂人并列 # 最大n,随机
7-12 求整数序列中出现次数最多的数
输入样例:
10 3 2 -1 5 3 4 3 0 3 2
输出样例:
3 4
代码:
方法一
s = input().split() num = list(set(s[1:])) num.sort() dict1 = {} for i in num: if i not in dict1.keys(): dict1[i] = s.count(i) max_key = max(dict1, key=dict1.get) print("%s %d" % (max_key, dict1[max_key]))
方法二
a = input().split() #在一行中输入多个数 i = int(a[0]) + 1 #确定第一个数字大小即是列表应有的长度 del a[i:] #删除多余输入的数字 del a[0] #删除第一个数字 #现在a中存储的即为要求序列 ans = 0 count = 0 for i in a: #从第一个数字开始遍历(第一个循环) num = 0 for j in a: #从第一个数字开始遍历(第二个循环) if j == i: #如果两个数字相等,那么这个数字的数量加一 num += 1 if count < num: #判断数量最多的数字并存储 count = num ans = i print(ans, end = ' ') print(count)
7-13 jmu-Java&Python-统计文字中的单词数量并按出现次数排序
输入样例1
failure is probably the fortification in your pole it is like a peek your wallet as the thief when you are thinking how to spend several hard-won lepta when you are wondering whether new money it has laid background because of you then at the heart of the most lax alert and most low awareness and left it godsend failed !!!!!
输出样例1
46 the=4 it=3 you=3 and=2 are=2 is=2 most=2 of=2 when=2 your=2
输入样例2
Failure is probably The fortification in your pole! It is like a peek your wallet as the thief when You are thinking how to. spend several hard-won lepta. when yoU are? wondering whether new money it has laid background Because of: yOu?, then at the heart of the Tom say: Who is the best? No one dare to say yes. most lax alert and! most low awareness and* left it godsend failed !!!!!
输出样例2
54 the=5 is=3 it=3 you=3 and=2 are=2 most=2 of=2 say=2 to=2
代码:
(这题的try except有点多余,我没改)
import ast t="" dict1={} sum=0 while True: try: t=input() if t=="!!!!!": break t = str(t).replace('!',' ').replace(".",' ').replace(",",' ').replace(":",' ').replace("*",' ').replace("?",' ') t=t.split(" ") except: pass for i in t: if i.lower() in dict1 : dict1[i.lower()] += 1 elif i.lower() not in dict1 and i!="": dict1[i.lower()] = 1 dict1 = list(dict1.items()) dict1.sort(key=lambda dict1 : dict1[0]) dict1.sort(key=lambda dict1 : dict1[1],reverse=True) print(len(dict1)) dict1=dict1[0:10] for key2 ,values2 in dict1: print("%s=%s"%(key2,values2))
看看别人写的代码,学习一下更好的方法。
本题需要注意的要点:
需要对字符串的这些 ! . , : * ? 标点符号进行处理,需要用到字符串自带的replace()函数.
在处理字典的键值对的问题的时候,我们往往需要结合元组和列表的相关操作来进行。
使用sorted()函数对元素是元组的列表进行排序的时候,排序的参考变量只会是每个元组中的第一个元素。以及sorted()默认升序排列,要求降序的话,需要加上reverse=True。
zip()函数能灵活按 (键:值) 或者 (值:键) 的循序来整合处理字典,方便分别依照键或依照值为标准来进行排序。
本题最大的难点在于需要进行二次排序:
需要优先进行按值排序后,还要对值相同的切片进行按键排序,其中出现了控制切片排序的方法。
在更新切片序列的时候,别忘记要带上当前的元组所包含的值!
由于切片融入新列表的触发条件是值发生改变,而最后一块值的切片无法在循环中融入新列表,所以需要在循环外额外加入!
1
# 处理多个空格间隔的函数: def HandleBlock(list): new_list = [] for item in list: if item != "": new_list.append(item) return new_list removechars = "!.,:*?" total_list = [] line = "" while line != "!!!!!": line = input() if len(line) == 0: pass elif line != "!!!!!": # 去除 "!.,:*?" for char in removechars: line = line.replace(char, ' ') # 去除空格及做列表拆分 line = line.split(" ") line = HandleBlock(line) # 统一以小写状态输入 for item in line: total_list.append(item.lower()) else: pass # 登记出现的个数 # 初始化统计表 statistical_table = {} for item in total_list: statistical_table[item] = 0 # 统计 for item in total_list: statistical_table[item] += 1 # 第一层按次数排序 temp_table = list(zip(statistical_table.values(), statistical_table.keys())) ordered_table = sorted(temp_table, reverse=True) # 第二层次数相同按键的字母序排序 final_ordered_table = [] cut_list = [] cur_times = ordered_table[0][0] for item in ordered_table: if item[0] == cur_times: cut_list.append((item[1], item[0])) else: ordered_cut_list = sorted(cut_list) final_ordered_table += ordered_cut_list # 更新当前的值 cur_times = item[0] cut_list = [] cut_list.append((item[1], item[0])) ordered_cut_list = sorted(cut_list) final_ordered_table += ordered_cut_list # 输出 numb = len(final_ordered_table) print(numb) for i in range(10): print(f"{final_ordered_table[i][0]}={final_ordered_table[i][1]}")
2
from functools import cmp_to_key def cmpkey2(x, y): if x[1] > y[1]: return 1 elif x[1] < y[1]: return -1 elif x[0] > y[0]: return -1 elif x[0] < y[0]: return 1 return 0 text = "" while True: s = input() if s == '!!!!!': break text += ' ' text += s text = text.lower() for ch in '!.,:*?': text = text.replace(ch, ' ') cnt = {} for word in text.split(): cnt[word] = cnt.get(word, 0) + 1 items = list(cnt.items()) items.sort(key=cmp_to_key(cmpkey2), reverse=True) print(len(items)) for i in range(10): if i >= len(items): break key, val = items[i] print("{}={}".format(key, val))
7-14 jmu-Java&Python-统计一段文字中的单词个数并按单词的字母顺序排序后输出
输入样例
Failure is probably the fortification in your pole It is like a peek your wallet as the thief when you are thinking how to spend several hard-won lepta when you Are wondering whether new money it has laid background Because of you, then at the heart of the most lax alert and most low awareness and left it godsend failed !!!!!
输出样例
49 Are Because Failure It a alert and are as at
代码:
t = "" dict1 = {} sum = 0 while True: t = input() if t == "!!!!!": break t = t.split(" ") for i in t: if i in dict1: dict1[i] += 1 elif i not in dict1 and i != "": dict1[i] = 1 dict1 = list(dict1.items()) dict1.sort(key=lambda dict1: dict1[0]) print(len(dict1)) dict1 = dict1[0:10] for key2, values2 in dict1: print(key2)
7-15 查单词所在页码
输入样例:
4 cungneh 19 wyd 17 aqkj 2 olckomm 15 4 wyd aqkj cungneh olckomm
输出样例:
17 2 19 15
代码:
方法1
N = int(input()) dict1 = {} for i in range(N): danci = input() num = input() dict1[danci] = num M = int(input()) for i in range(M): x = input() print(dict1[x])
方法2
n=int(input()) p=[] for i in range(n): m=input().split() s=input().split() m.extend(s) p.append(m) x=int(input()) for j in range(x): l=input() for k in range(len(p)): if p[k][0]==l: print(p[k][1])