文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Python基础练习100题 ( 81

2023-01-31 08:17

关注

昨天和大家分享了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<i]
print(li)

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:*

abcdefgabc
Then, 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

谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯