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 | l = [12,24,35,70,88,120,155] |
运行结果:
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 | l1 = [0 for i in range (3)] # 一维 |
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 | l = [12,24,35,70,88,120,155] |
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 | l = [12,24,35,70,88,120,155] |
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 | l = [5,6,77,45,22,12,24] |
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 | subject = ["I", "You"] |
7
Please write a program to shuffle and print the list [3,6,7,8].
Use shuffle() function to shuffle a list
分析:shuffle()可将列表顺序变成随机,并输出
1 | import random |
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 | from timeit import Timer |
9
Use zlib.compress() and zlib.decompress() to compress and decompress a string.
分析:用zlib下的函数进行字符串的压缩和解压
1 | import zlib |
运行错误,代码有问题
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 | import random |
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 | import random |
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 | import random |
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 | import random |
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 | import random |
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 | import random |
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论