gpt4 book ai didi

string - 按 J 中的长度对字符串中的单词进行排序

转载 作者:行者123 更新时间:2023-12-03 20:23:08 26 4
gpt4 key购买 nike

我有以下字符串:
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 splitstringregexes可能有帮助。
   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

关于string - 按 J 中的长度对字符串中的单词进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66950473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com