python作业02

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15

1

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].

Use list comprehension to delete a bunch of element from a list. Use enumerate() to get (index, value) tuple.

分析:使用推导式删除第0,4,5位元素

1
2
3
l =  [12,24,35,70,88,120,155]
l= [data for i,data in enumerate(l) if(i!=0 and i!=4 and i!=5)]
print (l)

运行结果:

image-20230916025325208

2

By using list comprehension, please write a program generate a 358 3D array whose each element is 0.

Use list comprehension to make an array.

分析:用三个循环嵌套生成三维数组

1
2
3
4
l1 = [0 for i in range (3)]  # 一维
l2 = [l1 for i in range (5)] # 二维
l3 = [l2 for i in range (8)] # 三维
print (l3)
image-20230916030018811

3

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].

Use list comprehension to delete a bunch of element from a list. Use enumerate() to get (index, value) tuple.

分析:用推导式删去偶数位的元素

1
2
3
l =  [12,24,35,70,88,120,155]
l= [data for i,data in enumerate(l) if i%2!=0]
print (l)

image-20230916030128818

4

By using list comprehension, please write a program to print the list after removing delete numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155].

Use list comprehension to delete a bunch of element from a list.

分析:推导式遍历列表,排除能被5或7整除的数

1
2
3
l = [12,24,35,70,88,120,155]
l = [x for x in l if (x%5!=0 and x%7!=0)]
print(l)

image-20230916030305116

5

Please write a program to print the list after removing delete even numbers in [5,6,77,45,22,12,24].

Use list comprehension to delete a bunch of element from a list

分析:使用推导式排除偶数

1
2
3
l = [5,6,77,45,22,12,24]
l = [x for x in l if x%2==1]
print(l)

image-20230916030329733

6

Please write a program to generate all sentences where subject is in [“I”, “You”] and verb is in [“Play”, “Love”] and the object is in [“Hockey”,”Football”].

Use list[index] notation to get a element from a list.

分析:用三层循环嵌套,遍历每一种组合

1
2
3
4
5
6
7
8
subject = ["I", "You"]
verb = ["Play", "Love"]
ob = ["Hockey","Football"]
for x in subject:
for y in verb:
for z in ob:
sentence =x+' '+y+ ' '+z
print(sentence)

image-20230916030355896

7

Please write a program to shuffle and print the list [3,6,7,8].

Use shuffle() function to shuffle a list

分析:shuffle()可将列表顺序变成随机,并输出

1
2
3
4
5
import random
l = [3,6,7,8]
random.shuffle(l)
print (l)

image-20230916030427024

8

Please write a program to print the running time of execution of “1+1” for 100 times.

Use timeit() function to measure the running time.

分析:timeit()可以测试语句的运行时间

1
2
3
4
from timeit import Timer
t = Timer("for i in range(100):1+1")
print (t.timeit())

image-20230916030449623

9

Use zlib.compress() and zlib.decompress() to compress and decompress a string.

分析:用zlib下的函数进行字符串的压缩和解压

1
2
3
4
5
import zlib
s = "B21031618"
s1 = zlib.compress(s,1)
s2= zlib.decompress(s1)
print (s2)

运行错误,代码有问题

10

Please write a program to randomly print a integer number between 7 and 15 inclusive.

Use random.randrange() to a random integer in a given range.

分析:randrange() 方法返回指定递增基数集合中的一个随机数,基数默认值为1。

1
2
import random
print(random.randrange(5,16,1))

image-20230916030654004

11

Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive.

Use random.sample() to generate a list of random values.

分析:sample(l,k)从列表l中随机选取k个值组成新的列表

1
2
3
4
import random
l1 = [x for x in range(1,1001) if (x%5==0 and x%7==0)]
l2=random.sample(l1,5)
print(l2)

image-20230916030736287

12

Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive.

Use random.sample() to generate a list of random values.

分析:同11,加上偶数的筛选条件

1
2
import random
print (random.sample([i for i in range(100,201) if i%2==0], 5))

image-20230916030813207

13

Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive.

Use random.sample() to generate a list of random values.

分析:同11

1
2
import random
print( random.sample([x for x in range(100,201)],5))

image-20230916030843283

14

Please write a program to output a random number, which is divisible by 5 and 7, between 0 and 100 inclusive using random module and list comprehension.

Use random.choice() to a random element from a list

分析:从0~100内任选一个可以被5和7整除的数

1
2
import random
print(random.choice([x for x in range(101)if (x % 5==0 and x %7==0)] ))

image-20230916030922472

15

Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension

Use random.choice() to a random element from a list.

分析:从0~10任选一个偶数

1
2
import random
print(random.choice([x for x in range(11) if x % 2 == 0]))

image-20230916030950174


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论