首页 \ 问答 \ Python数组切片 - 如何实现二维数组切片?(Python array slicing — How can 2D array slicing be implemented?)

Python数组切片 - 如何实现二维数组切片?(Python array slicing — How can 2D array slicing be implemented?)

我想知道如何在Python中实现二维数组切片?

例如,

arr是自定义类2D数组的一个实例。

如果我想在这个对象上启用2D切片语法,如下所示:

arr[:,1:3] #retrieve the 1 and 2 column values of every row

要么

arr[,:3] #retrieve the 1 and 2 column values of every row

用法和语法就像numpy.array。 但是这种功能怎么能够自己实现呢?

PS:

我想到的是:

对于第一种情况, [:,1:3]部分就像是两片的元组

但是,对于第二种情况, [,1:3]似乎很神秘。


I am wondering how 2D array slicing can be implemented in Python?

For example,

arr is an instance of a self-defined class 2D array.

if I want to enable 2D slicing syntax on this object like this:

arr[:,1:3] #retrieve the 1 and 2 column values of every row

or

arr[,:3] #retrieve the 1 and 2 column values of every row

The usage and syntax is just like numpy.array. But how can this kind of functionality be realized on our own?

PS:

What I have in mind:

for the first case, the [:,1:3] part is like a tuple of two slices

However, for the second case [,1:3] appears to be quite mysterious.


原文:https://stackoverflow.com/questions/16096753
更新时间:2023-01-07 19:01

最满意答案

这可能不是最酷的方式,但我认为它很漂亮。

Laravel有一个名为Form::macro的功能,可以让你制作可重用的代码片段。 你可以定义一个宏的所有类型的地方,但我只是在我的routes.php文件中打了我的这个真正的快速。

Form::macro('errorMsg', function($field, $errors){
    if($errors->has($field)){
        $msg = $errors->first($field);
        return "<span class=\"error\">$msg</span>";
    }
    return '';
});

然后在窗体中使用,将错误消息传递给宏:

{{ Form::label('first_name', 'First Name:') }}
{{ Form::text('first_name') }}
{{ Form::errorMsg('first_name', $errors) }}
{{-- where $errors is your Illuminate\Support\MessageBag object --}}

为了获得更多的技术,你可以在Form::macro使用Form::objects ,如下所示:

Form::macro('textError', function($field, $label, $errors){
    $label_html = Form::label($field, $label);
    $text_html = Form::text($field);
    $msg_html = '';

    if($errors->has($field)){
            $msg_html.= '<span class="error">';
            $msg_html.= $errors->first($field);
            $msg_html.= '</span>';
    }

    return $label_html.$text_html.$msg_html;
});

然后你在每个输入1行:

{{ Form::textError('first_name', 'First Name:', $errors) }}

您需要为密码,textarea等创建其他宏(这就是为什么我只使用第一个示例;每个输入只有几行代码,并提供所有输入类型。)

如果你想对错误进行输入,例如。 一个红色的边框,你可能会想要第二个例子,然后将它包裹在你的div.form-group或whatevers中。 无论如何,选择wazoo。

更新:在宏中获取错误的一种更简单的方式是通过Session::get('errors');来访问它们Session::get('errors'); 像这样:

Form::macro('errorMsg', function($field){//yay! we don't have to pass $errors anymore
    $errors = Session::get('errors');

    if($errors && $errors->has($field)){//make sure $errors is not null
        $msg = $errors->first($field);
        return "<span class=\"error\">$msg</span>";
    }
    return '';
});

This may not be the coolest way to do this but I think it's pretty slick.

Laravel has a feature called Form::macro that allows you to sort of make reusable code snippets. You can define a macro all kinds of places but I just slapped mine in my routes.php file to get this going real quick.

Form::macro('errorMsg', function($field, $errors){
    if($errors->has($field)){
        $msg = $errors->first($field);
        return "<span class=\"error\">$msg</span>";
    }
    return '';
});

Then to use in a form, pass the macro your error messages:

{{ Form::label('first_name', 'First Name:') }}
{{ Form::text('first_name') }}
{{ Form::errorMsg('first_name', $errors) }}
{{-- where $errors is your Illuminate\Support\MessageBag object --}}

To get even more tech, you can use Form::objects in a Form::macro like so:

Form::macro('textError', function($field, $label, $errors){
    $label_html = Form::label($field, $label);
    $text_html = Form::text($field);
    $msg_html = '';

    if($errors->has($field)){
            $msg_html.= '<span class="error">';
            $msg_html.= $errors->first($field);
            $msg_html.= '</span>';
    }

    return $label_html.$text_html.$msg_html;
});

Then you're at 1 line per input:

{{ Form::textError('first_name', 'First Name:', $errors) }}

You'd need to make other macros for password, textarea, etc. (which is why I just use the first example; it's only a couple more lines of code per input and serves all input types.)

If you wanted to style your input on error, eg. a red border, you'd probably want the second example then wrap it in your div.form-group or whatevers. Anyway, options out the wazoo.

UPDATE: An even slicker way to get errors in macros is to access them via Session::get('errors'); like so:

Form::macro('errorMsg', function($field){//yay! we don't have to pass $errors anymore
    $errors = Session::get('errors');

    if($errors && $errors->has($field)){//make sure $errors is not null
        $msg = $errors->first($field);
        return "<span class=\"error\">$msg</span>";
    }
    return '';
});

相关问答

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。