【Python Practice】Day 20 Question 80-84

'''
@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()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值