摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载题我的运
刷题继续
昨天和大家分享了71-80题,今天继续来刷81~90题
Question 81:By using list comprehension, please write a program to print the list after removing numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155].
li = [12,24,35,70,88,120,155] li = [x for x in li if x % 35!=0] print(li)Question 82:
By using list comprehension, please write a program to print the list after removing the 0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155].
li = [12,24,35,70,88,120,155] li = [li[i] for i in range(len(li)) if i%2 != 0] print(li)解法二
li = [12,24,35,70,88,120,155] li = [x for (i,x) in enumerate(li) if i%2!=0] print(li)Question 83:
By using list comprehension, please write a program to print the list after removing the 2nd - 4th numbers in [12,24,35,70,88,120,155].
li = [12,24,35,70,88,120,155] li = [x for (i,x) in enumerate(li) if i<3 or 4 Question 84:By using list comprehension, please write a program generate a 3*5*8 3D array whose each element is 0.
解法一array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)] print(array)Question 85:By using list comprehension, please write a program to print the list after removing the 0th,4th,5th numbers in [12,24,35,70,88,120,155].
解法一li = [12,24,35,70,88,120,155] li = [li[i] for i in range(len(li)) if i not in (0,4,5)] print(li)Question 86:By using list comprehension, please write a program to print the list after removing the value 24 in [12,24,35,24,88,120,155].
解法一li = [12,24,35,24,88,120,155] li.remove(24) # this will remove only the first occurrence of 24 print(li)解法二li = [12,24,35,24,88,120,155] li = [x for x in li if x!=24] # Delete all 24 print(li)Question 87:With two given lists [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a program to make a list whose elements are intersection of the above given lists.
解法一set1=set([1,3,6,78,35,55]) set2=set([12,24,35,24,88,120,155]) result = set1.intersection(set2) print(result)解法二set1=set([1,3,6,78,35,55]) set2=set([12,24,35,24,88,120,155]) result = set1 & set2 print(result)Question 88:With a given list [12,24,35,24,88,120,155,88,120,155], write a program to print this list after removing all duplicate values with original order reserved.
解法一li=[12,24,35,24,88,120,155,88,120,155] result = set(li) #> Python 3.6 Set keep the order test print(result)解法二def removeDuplicate( li ): seen = {} for item in li: if item not in seen: seen[item] = True yield item li = [12, 24, 35, 24, 88, 120, 155, 88, 120, 155] ans = list(removeDuplicate(li)) print(ans)解法三li = [12,24,35,24,88,120,155,88,120,155] for i in li: if li.count(i) > 1: li.remove(i) print(li)Question 89:Define a class Person and its two child classes: Male and Female. All classes have a method "getGender" which can print "Male" for Male class and "Female" for Female class.
解法一class Person(object): def getGender( self ): return "Unknown" class Male( Person ): def getGender( self ): return "Male" class Female( Person ): def getGender( self ): return "Female" # Test aMale = Male() aFemale= Female() print(aMale.getGender()) print (aFemale.getGender())Question 90:Please write a program which count and print the numbers of each character in a string input by console.*Example:
If the following string is given as input to the program:*abcdefgabcThen, the output of the program should be:a,2 c,2 b,2 e,1 d,1 g,1 f,1解法一s=input() dic = {word:s.count(word) for word in s} for k, v in dic.items(): print(f"{k},{v}")解法二s = input() for letter in range(ord("a"),ord("z")+1): # ord() gets the ascii value of a char letter = chr(letter) # chr() gets the char of an ascii value cnt = s.count(letter) if cnt > 0: print("{},{}".format(letter,cnt))源代码下载这十道题的代码在我的github上,如果大家想看一下每道题的输出结果,可以点击以下链接下载:
Python 81-90题
我的运行环境Python 3.6+,如果你用的是Python 2.7版本,绝大多数不同就体现在以下3点:
raw_input()在Python3中是input()
print需要加括号
fstring可以换成.format(),或者%s,%d
谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/44000.html
摘要:刷题继续昨天和大家分享了题,今天继续来刷最后的题解法一解法二解法一解法二解法一鸡兔同笼解法一解法一解法二解法一解法二默认就是 刷题继续 昨天和大家分享了81-90题,今天继续来刷最后的91-100题 Question 91: Please write a program which accepts a string from console and print it in rever...
摘要:笨办法学第版结构非常简单,共包括个习题,其中个覆盖了输入输出变量和函数三个主题,另外个覆盖了一些比较高级的话题,如条件判断循环类和对象代码测试及项目的实现等。最后只想说,学习不会辜负任何人,笨办法学 内容简介 《笨办法学Python(第3版)》是一本Python入门书籍,适合对计...
摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法二解法一解法一解法二解法一解法一解法二解法一解法一解法二解法一解法二解法一解法二解法三解法一解法二源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下 刷题继续 昨天和大家分享了61-70题,今天继续来刷71~80题 Question 71: Please write a program to out...
摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载题我的运行环境如果你 刷题继续 昨天和大家分享了21-30题,今天继续来刷31~40题 Question 31: Define a function which can pr...
摘要:刷题继续昨天和大家分享了题,今天继续来刷题解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代码下载这十道题的代码在我的上,如果大家想看一下每道题的输出结果,可以点击以下链接下载 刷题继续 昨天和大家分享了41-50题,今天继续来刷51~60题 Question 51: Write a function to compute 5/0 and use ...
阅读 1228·2021-11-15 11:37
阅读 2243·2021-09-30 09:55
阅读 4481·2021-09-22 15:51
阅读 3741·2021-09-22 15:46
阅读 2766·2019-08-30 15:52
阅读 422·2019-08-29 16:20
阅读 2888·2019-08-29 15:12
阅读 1129·2019-08-26 18:27