如何在 perl 6 中“展平"列表列表? [英] How do I “flatten” a list of lists in perl 6?

查看:51
本文介绍了如何在 perl 6 中“展平"列表列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想要 a、b 和 c 中 2 个字母的所有排列.

Let's say I want all permutations of 2 letters out of a, b and c.

我能做到:

my @perm = <a b c>.combinations(2)».permutations;
say @perm;
# [((a b) (b a)) ((a c) (c a)) ((b c) (c b))]

这很接近,但不完全是我需要的.我如何展平"它以便我得到:

which is close, but not exactly what I need. How do I "flatten" this so that I get:

# [(a b) (b a) (a c) (c a) (b c) (c b)]

?

推荐答案

另见 实现我(OP)想要的更好的方法".

另见一些可能的解决方案";回答我怎样才能完全展平 Raku 列表(列表(列表)......)"?问题.

my \perm = <a b c>.combinations(2)».permutations;
say perm;       # (((a b) (b a)) ((a c) (c a)) ((b c) (c b)))
say perm[*];    # (((a b) (b a)) ((a c) (c a)) ((b c) (c b)))
say perm[*;*];  # ((a b) (b a) (a c) (c a) (b c) (c b))
say perm[*;*;*] # (a b b a a c c a b c c b)

注意事项

我使用了一个非印记变量,因为我认为对于那些不了解 Raku 的人来说,这更清楚一些.

Notes

I used a non-sigil'd variable because I think it's a bit clearer what's going on for those who don't know Raku.

我没有将下标附加到原始表达式中,但我可以:

I didn't append the subscript to the original expression but I could have:

my \perm = <a b c>.combinations(2)».permutations[*;*];
say perm;       # ((a b) (b a) (a c) (c a) (b c) (c b))

这篇关于如何在 perl 6 中“展平"列表列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆