栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

LinkCode部分python题目

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LinkCode部分python题目

目录

T1:国际象棋:

​编辑

         官方题解:(采用numpy的切片)

         其他较好题解:


T1:国际象棋:

         官方题解:(采用numpy的切片)
import numpy as np
def create_metrix() -> np.ndarray:
    chessBoard = np.zeros((8, 8), dtype=int)
    chessBoard[1::2, ::2] = 1
    chessBoard[::2, 1::2] = 1
    return chessBoard  

关键在于: 维度的理解(第0维表示x轴横向,第1维表示y轴纵向) 

相关切片的讲解文章https://blog.csdn.net/weixin_36670529/article/details/111307004

         其他较好题解:
import numpy as np
def create_metrix() -> np.ndarray:
    metrix = np.zeros((8, 8), dtype=int)
    for x in range(8):
        for y in range(8):
            if (x+y) % 2 != 0:
                metrix[x][y] = 1
    return metrix
T2:subplot绘图

 

 官方题解:
# coding: utf-8
import matplotlib.pyplot as plt


def subplot_image(input_dir: str):
    """
    :param input_dir: The direction of input image, like: '../input/img.png'
    """
    # write your code here
    img=plt.imread(input_dir)
    for i in range(4):
        k=i+1
        plt.subplot(2,2,k)
        plt.imshow(img)
 其他解法:
# coding: utf-8
import matplotlib.pyplot as plt


def subplot_image(input_dir: str):
    """
    :param input_dir: The direction of input image, like: '../input/img.png'
    """
    # write your code here
    data = plt.imread(input_dir)
    
    # way-1
    fig,axs = plt.subplots(2,2)
    axs[0,0].imshow(data)
    axs[0,1].imshow(data)
    axs[1,0].imshow(data)
    axs[1,1].imshow(data)

    #way-2
    # plt.subplot(2,2,1)
    # plt.imshow(data)
    # plt.subplot(2,2,2)
    # plt.imshow(data)
    # plt.subplot(2,2,3)
    # plt.imshow(data)
    # plt.subplot(2,2,4)
    # plt.imshow(data)

    # way-3
    # fig = plt.figure()
    # ax1 = fig.add_subplot(221)
    # ax1.imshow(data)
    # ax2 = fig.add_subplot(222)
    # ax2.imshow(data)
    # ax3 = fig.add_subplot(223)
    # ax3.imshow(data)
    # ax4 = fig.add_subplot(224)
    # ax4.imshow(data)

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000750.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号