专栏首页python3Python基础练习100题 ( 81

Python基础练习100题 ( 81

刷题继续

昨天和大家分享了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 3.6+,如果你用的是Python 2.7版本,绝大多数不同就体现在以下3点:

  • raw_input()在Python3中是input()
  • print需要加括号
  • fstring可以换成.format(),或者%s,%d

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • python 爬取 instagram 用户的关注列表

    这是很久之前我的房东找我帮忙爬 instagram 上面某个用户的关注列表,一开始我想着减低难度好给他使用,于是尝试了 webscraper,后羿采集器去爬取,...

    伪君子
  • Pytest跳过执行之@pytest.mark.skip()详解

    在我们自动化测试过程中,经常会遇到功能阻塞、功能未实现、环境等一系列外部因素问题导致的一些用例执行不了,这时我们就可以用到跳过skip用例,如果我们注释...

    橙子探索测试
  • 一日一技:在 Python 里面,如何正确判断两个浮点数相等

    在以前的文章里面,我们已经讲到过,不仅仅是 Python,很多编程语言里面,浮点数都不一定是精确的。最常被用来作为例子的是:0.1 + 0.2。在 Python...

    青南
  • Detectron2学习五:build_backbone_model配置及实现流程

    本文主要讲build_backbone_model的配置及创建流程,目的则是希望大家看完本章节后能够对detectron2中模型创建及使用有清晰的认识,便于后续...

    CV_Learner
  • 单元测试框架选择

    单元测试是指,对软件中的最小可测试单元在与程序其他部分相隔离的情况下进行检查和验证的工作,这里的最小可测试单元通常是指函数或者类。

    用户2149234
  • C++惯用法之消除垃圾收集器-资源获取即初始化方法(RAII)

    在C语言中,有三种类型的内存分配:静态、自动和动态。静态变量是嵌入在源文件中的常数,因为它们有已知的大小并且从不改变,所以它们并不那么有趣。自动分配可以被认为是...

    公众号guangcity
  • 为什么要学Python编程?(附Python学习路线)

    在此,估计不少开发者都会予以反驳,自己明明就没有选择 Python,不能一概而论。下面,我们就用数据一窥如今最流行的编程语言。

    AI科技大本营
  • mysql-python:_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such fil

    使用python 操作mysql 在安装MySQL-python 时报错: _mysql.c _mysql.c(42) : fatal error C1083:...

    似水的流年
  • Pytest自定义标记mark及指定文件/类/方法/用例执行

    pytest.main(['-s','test01.py','-m=test'])

    橙子探索测试
  • python补充之进制转换、exec、eval、compile

    计算指定表达式的值。也就是说它要执行的python代码只能是单个表达式(注意eval不支持任何形式的赋值操作),而不能是复杂的代码逻辑。

    用户6817597

扫码关注云+社区

领取腾讯云代金券