Python语言上机题实现方法(持续更新...) (3)

【代码】:

a = [11,22,33] b = [22,33,44] c = [] d = [] e = [] f = [] for i in a: for j in b: if i == j: c.append(i) d = [i for i in a if not i in b] e = [i for i in b if not i in a] f = d + e print(c,d,e,f)

16.编写Python程序计算下列数学表达式的结果并输出,小数点后保留3位,输出占10位,空位用‘*’填充,右对齐。
$$
x =sqrt((pow(3,4) + 5 + pow(6,7)) / 8)
$$
代码】:

import math a = (pow(3,4) + 5 + pow(6,7)) / 8 x = math.sqrt(a) print("%.3f" % x)

17.补充程序,分别输出字符串s中汉字和标点符号的个数。

s = "学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?"

n = 0 #汉字个数

m = 0 #标点符号个数

​ #在这里补充代码,可以多行

print("字符数为{},标点符号数为{}。".format(n, m))

【代码】:

s = "学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?" n = 0 m = 0 n = s.count(',') + s.count('?') m = len(s) - n print("字符数为{},标点符号数为{}。".format(m,n))

18.通过键盘输入一串小写字母(a~z)组成的字符串,编写一个测试程序,将字符串中连续出现的重复字符删去(即在一个字符串中,如果遇到连续重复的字符只出现一次),然后输出处理后的字符串。例如:str1="aabbccddaabbccdd",输出结果为:"abcdabcd"。

输入样例:aabcccccaaa 输出样例:abca

【代码】:

str1=input() a=list(str1) b=a[0] c=[] c.append(b) for i in range(1,len(a)): if a[i]!=a[i-1]: x=a[i] c.append(x) for i in c: print(i,end="")

19.编写程序,运行后输入任意长度正整数,然后输出每位上的数字,并且使用英文逗号分隔。例如输入123,输出1,2,3

【代码】:

Num1 = int(input()) Num2 = [] b = len(str(Num1)) while(b != 0): a = Num1 % 10 Num2.insert(0,str(a)) Num1 = int(Num1 / 10) b = b-1 Num3 = tuple(Num2) c = "," c.join(Num3) print(c.join(Num3))

20.编写程序,从给定字符串中查找某指定的字符。

输入格式:输入的第一行是一个待查找的字符。第二行是一个以回车结束的非空字符串(不超过80个字符)。输出格式:如果找到,在一行内按照格式“index = 下标”(等号两边都有英文的空格)输出该字符在字符串中所对应的最大下标(下标从0开始);否则输出"Not Found"。

输入样例1: 输入样例2: m a programming 1234 输出样例1: 输出样例2: Index = 7 Not Found

【代码】:

a = input() b = input() s1 = list(b) if a in s1: print("index =",b.rindex(a)) else: print("Not Found")

21.输入一个字符串,判断该字符串是否为回文。回文就是字符串中心对称,从左向右读和从右向左读的内容是一样的。输入格式:输入在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。

输出格式:输出在第1行中输出字符串。如果它是回文字符串,在第2行中输出Yes,否则输出No。

输入样例1: 输入样例2: level 1 + 2 = 2 + 1 = 输出样例1: 输出样例2: level 1 + 2 = 2 + 1 = Yes No

【代码】:

s1 = input() s2 = reversed(list(s1)) if list(s1) == list(s2): print("Yes") else: print("No")

22.【字典1】输入一个列表,要求列表中的每个元素都为正整数且列表包含的元素个数为偶数;将列表中前一半元素保存至字典的第一个键值1中,后一半元素保存至第二个键值2中。

输入格式: 共一行,列表中的元素值,以空格隔开。输出格式:共一行,以字典的形式打印结果。 输入样例:1 2 3 4 输出样例:{'1': [1,2], '2': [3,4]}

【代码】:

a = list(map(int,input().split())) n = len(a) v1 = a[:int(len(a)/2)] v2 = a[int(len(a)/2):] print(dict({'1':v1,'2':v2}))

23.【字典2】阅读下面这段文字

Python includes two operations for sorting. The method sort() in the built-in list data type

rearranges the items in the underlying list into ascending order, much like merge.sort(). In

contrast, the built-in function sorted() leaves the underlying list alone; instead, it returns a new list containing the items in ascending order.

理解上述文字后,对其进行词频统计,再按照词频和字典顺序排序

提示1、词频是主关键字,字典顺序是次关键字 2、字典顺序不考虑大小写,可以使用字符串的lower方法

【代码】:

txt = "Python includes two operations for sorting. The method sort() in the built-in list data typerearranges the " \ "items in the underlying list into ascending order, much like merge.sort(). Incontrast, the built-in function " \ "sorted() leaves the underlying list alone; instead, it returns a new list containing the items in ascending " \ "orderfor" words = txt.split() counts = {} for word in words: counts[word] = counts.get(word,0) + 1 items = list(counts.items()) items.sort(key=lambda x:x[1],reverse=True) for i in range(10): word,count = items[i] print("{0:<10}{1:>5}".format(word,count))

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwwxds.html