如何解决按 J 中的长度对字符串中的单词进行排序
我有以下字符串:
s=:'我们什么时候可以跳舞'
我可以用这个找到每个单词的长度:
# each ;:s
我可以按升序/降序对长度进行排序:(/:~) # each ;:s
这给了我盒装输出。
但是我如何打印这些文字?
解决方法
您已经在使用 /:~
,它是二元 Reflex 的 Sort Up; /:~ y
等价于 y /: y
。这是一个方便的快捷方式,但在这种情况下,您跳过的是您正在寻找的解决方案。 /:
(不带反射)根据将导致排序的 x
的排列排列其 y
。顺便说一句,“将导致排序 y
”的排列是 monadic /:
(Grade Up) 是为了。
]w=:;:'when can we dance'
+----+---+--+-----+
|when|can|we|dance|
+----+---+--+-----+
/: w
1 3 2 0
(/: w) { w
+---+-----+--+----+
|can|dance|we|when|
+---+-----+--+----+
w /: w
+---+-----+--+----+
|can|dance|we|when|
+---+-----+--+----+
所以要按其他东西对 w
进行排序,例如其项目的长度,只需提供其他东西作为 Sort Up 的 y
:
w /: # each w
+--+---+----+-----+
|we|can|when|dance|
+--+---+----+-----+
(/: # each) w NB. a hook
+--+---+----+-----+
|we|can|when|dance|
+--+---+----+-----+
;:
为您提供字符串中的 J 词,这可能不是您想要的:
;: 'when can''t we dance'
|open quote
| ;:'when can''t we dance'
;.
Cut 家族或 stdlib's splitstring
或 regexes 中的一个可能会有所帮助。
13 : ';: ''when can we dance''' NB. 13 : can be a mnemonic
(<;._1 ' when can we dance')"_
<;._1 ' ','when can''t we dance'
+----+-----+--+-----+
|when|can't|we|dance|
+----+-----+--+-----+
' ' splitstring 'when can''t we dance'
+----+-----+--+-----+
|when|can't|we|dance|
+----+-----+--+-----+
s=:'when can''t we dance' NB. excess space
<;._1 ' ',s NB. empty boxes in result
+----+-----+++--+-----+
|when|can't|||we|dance|
+----+-----+++--+-----+
require 'regex'
'[\w'']+' rxall s
+----+-----+--+-----+
|when|can't|we|dance|
+----+-----+--+-----+
要将这些单词列表恢复为字符串,
w
+----+---+--+-----+
|when|can|we|dance|
+----+---+--+-----+
;: inv w
when can we dance
' ' joinstring w
when can we dance
,
朱利安在上面的回答中很好地解释了处理撇号的方法,但是如果您忽略它们的存在,我会使用 &.
(下)和 ;:
(单词)进行排序装箱拆箱后按长度排列。
s=:'when can we dance'
(/: # each) &. ;: s
we can when dance
(/: # each)
是根据每个长度对它们进行排序的钩子,&.;:
将它们装箱,然后在最后将它们拆箱以提供结果。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。