perl6 – 如何在“perl 6”中列出列表列表?
发布时间:2020-12-15 22:02:05 所属栏目:大数据 来源:网络整理
导读:假设我想要从a,b和c中的所有2个字母的排列. 我可以: my @perm = a b c.combinations(2)?.permutations;say @perm;# [((a b) (b a)) ((a c) (c a)) ((b c) (c b))] 这是接近,但不完全是我需要的. 我如何“扁平化”这样才能得到: # [(a b) (b a) (a c) (c a)
假设我想要从a,b和c中的所有2个字母的排列.
我可以: my @perm = <a b c>.combinations(2)?.permutations; say @perm; # [((a b) (b a)) ((a c) (c a)) ((b c) (c b))] 这是接近,但不完全是我需要的. # [(a b) (b a) (a c) (c a) (b c) (c b)] ? 解决方法
参见
“a better way to accomplish what I (OP) wanted”.
参见“Some possible solutions” answer to “How can I completely flatten a Perl 6 list (of lists (of lists) … )” question. 添加下标 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) 笔记 我使用了一个非标准变量,因为我认为对于那些不了解Perl 6的人来说,这是一个更清楚的事情. 我没有附加到原来的表达式,但我可以有: my perm = <a b c>.combinations(2)?.permutations[*;*]; say perm; # ((a b) (b a) (a c) (c a) (b c) (c b)) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |