'''
@Author: your name
@Date: 2020-07-26 10:50:36
@LastEditTime: 2020-07-26 11:13:32
@LastEditors: Please set LastEditors
@Description: In User Settings Edit
@FilePath: \vscode_py\day20.py
'''
import numpy as np
# Question 80
# Please write a program to print the list after removing even numbers in [5,6,77,45,22,12,24].
def Q80():
l=[i for i in [5,6,77,45,22,12,24] if i%2!=0]
print(l)
s=[str(i) for i in l]
print(','.join(s))
# Question 81
def Q81():
l=[12,24,35,70,88,120,155]
r=[i for i in l if i%35!=0]
print(r)
ans=list(filter(lambda x:x%35!=0 , l)) # filter的用法
print(ans)
# 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].
def Q82():
ori= [12,24,35,70,88,120,155]
indices=[0, 2, 4, 6]
new_list=[ori[i] for i in range(len(ori)) if i not in indices]
print(new_list)
# 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].
def Q83():
l=[12,24,35,70,88,120,155]
indices=[2,3,4]
ans=[l[i] for i in range(len(l)) if i not in indices]
print(ans)
# Question 84
# By using list comprehension, please write a program generate a 3*5*8 3D array whose each element is 0.
def Q84():
d1=[]
for i1 in range(3):
d2=[]
for i2 in range(5):
d3=[]
for i3 in range(8):
d3.append(0)
d2.append(d3)
d1.append(d2)
print(d1)
ans=np.array(d1)
print(ans.shape)
# 法2,列表comprehension
array=[[[0 for i in range(8)] for i in range(5)] for i in range(3)]
print(np.array(array).shape)
if __name__ == "__main__":
# Q80()
# Q81()
# Q82()
# Q83()
Q84()