在下午,我可以做以下工作,把所有物体列入一份列有特定财产的清单。 在此例子中,我填写了<代码>id。 www.un.org/Depts/DGACM/index_spanish.htm
ids = [ obj.id for obj in objs if obj.id > 100]
我怎么也这样做呢? 我认为,我想使用<条码>>地图<>,但我不知道如何有条件地从设定的原产地到设定的目的地。
在下午,我可以做以下工作,把所有物体列入一份列有特定财产的清单。 在此例子中,我填写了<代码>id。 www.un.org/Depts/DGACM/index_spanish.htm
ids = [ obj.id for obj in objs if obj.id > 100]
我怎么也这样做呢? 我认为,我想使用<条码>>地图<>,但我不知道如何有条件地从设定的原产地到设定的目的地。
<>代码>代码>栏目可退还原清单中每个要素的0或更多内容。 删除一个要素,仅退回空名单(
>):
my @ids = map { $_->id > 100 ? $_->id : () } @objs;
假设@objs
中的物体 有<条码>id属性和相关用户。 如果你想要直接接触,你也可以这样做:
my @ids = map { $_->{id} > 100 ? $_->{id} : () } @objs;
Or, you can just combine map
and grep
:
my @ids = map { $_->id } grep { $_->id > 100 } @objs;
# Or reverse the order to avoid calling $_->id twice:
my @ids = grep { $_ > 100 } map { $_->id } @objs;
我不敢肯定其中哪一部分会更有效率,但除非<代码>@objs确实大,这不大可能重要。
如果从物体中提取的数值昂贵,可以计算,那么,你可以收取测试和收益价值的价值:
my @vals = map { my $v = $_->expensive_method; $v > 100 ? $v : () } @objs;
使用<代码>grep,只退还符合条件的项目。 它以其他语文使用<条码>。
例如:
my @nums = (1, 50, 7, 105, 200, 3, 1000);
my @numsover100 = grep { $_ > 100 } @nums;
foreach my $num (@numsover100) {
print $num . "
";
}
使用地图和环形,是名单上的两倍。 建设你自己:
sub fancy_filter {
my ($map_block, $grep_block, @list) = @_;
my @results;
foreach my $item (@list) {
local $_ = $item;
if ($grep_block->()) {
push @results, $map_block->();
}
}
return @results;
}
my @ids = fancy_filter(
sub { $_->{id} }, # map block
sub { $_->{id} > 100 }, # grep block
@id_list,
)
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...