попробуйте создать такую функцию, как:
def selc_df(df, x=2):
return df.head(x).append(df.tail(x))
selc_df(df,2)
Пример:
>>> df
A B
0 1 345
1 2 366
2 3 299
3 3 455
4 4 879
5 5 321
6 5 957
7 6 543
Результат:
>>> def selc_df(df, x=2):
... return df.head(x).append(df.tail(x))
...
>>> selc_df(df,2)
A B
0 1 345
1 2 366
6 5 957
7 6 543
Поскольку Perl предполагает EXPR (ссылка хеша, например) вместо БЛОКА. Это должно работать (отметьте '+' символ):
my @a = ("a", "b", "c", "d", "e");
my %h = map { +"prefix-$_" => 1 } @a;
Я предпочитаю писать что как
my %h = map { ("prefix-$_" => 1) } @a;
показать намерение, что я возвращаю список с 2 элементами.
Я думаю это
map { ; "prefix-$_" => 1 } @a;
более идиоматично, до определения, что это - блок операторов, и не хеш касательно Вы просто сбрасываете его с ноги с пустым оператором.
От perldoc -f map
:
"{" starts both hash references and blocks, so "map { ..."
could be either the start of map BLOCK LIST or map EXPR, LIST.
Because perl doesn’t look ahead for the closing "}" it has to
take a guess at which its dealing with based what it finds just
after the "{". Usually it gets it right, but if it doesn’t it
won’t realize something is wrong until it gets to the "}" and
encounters the missing (or unexpected) comma. The syntax error
will be reported close to the "}" but you’ll need to change
something near the "{" such as using a unary "+" to give perl
some help:
%hash = map { "\L$_", 1 } @array # perl guesses EXPR. wrong
%hash = map { +"\L$_", 1 } @array # perl guesses BLOCK. right
%hash = map { ("\L$_", 1) } @array # this also works
%hash = map { lc($_), 1 } @array # as does this.
%hash = map +( lc($_), 1 ), @array # this is EXPR and works!
%hash = map ( lc($_), 1 ), @array # evaluates to (1, @array)
or to force an anon hash constructor use "+{"
@hashes = map +{ lc($_), 1 }, @array # EXPR, so needs , at end
and you get list of anonymous hashes each with only 1 entry.
Кроме того, другой способ сделать, что Вы делаете, инициализируя хеш, можно сделать как это:
my @a = qw( a b c d e );
my %h;
@h{@a} = ();
Это создаст undef записи для каждого из этих пяти ключей. Если Вы хотите дать им всем истинные значения, то сделайте это.
@h{@a} = (1) x @a;
Можно также сделать это явно с циклом;
@h{$_} = 1 for @a;