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 slicesHowever, for the second case
[,1:3]
appears to be quite mysterious.
原文:https://stackoverflow.com/questions/16096753
最满意答案
这可能不是最酷的方式,但我认为它很漂亮。
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 myroutes.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 aForm::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 ''; });
相关问答
更多-
检查您在security.yml排除/字体路径 security: firewalls: assets: pattern: ^/(_(profiler|wdt)|css|images|js|fonts)/ security: false 如果您直接访问字体( yourhost.com/fonts/glyphicons-halflings-regular.woff )会发生什么? 还要检查firebug或chrome检查器是否存在网络 ...
-
我认为你的问题归结为在设置工具提示时使用什么样的选择器,而且这个答案几乎是你想要的。 如果你想使用一个类来触发你的工具提示你可以这样做,例如: Hover me for a tooltip 然后,您可以触发所有连接的.link类附加为工具提示,如下所示: $('.link').tooltip() 现在,为了回答你的问题,为什么引导程序开发人员没有使用类来触发工具提示,因为它不 ...
-
引导2.3.2
- ...
- ...
- Lorem ipsum
- Phasellus iaculis
-
您需要设置表单的编码类型。