是否有Raku方法可以将哈希值与其对分配并拆分?
我目前正在尝试在数组中使用哈希来查找数组中每个特定项目的键和值。我能够做到这一点,当我没有对数组进行排序时,键和值都是分开的,但是当我创建一个排序的数组,例如:
my @sorted_pairs = %counts{$word}.sort(*.value);
它将值绑定在一起。是否有一种分类的哈希值方法可以将对分为数组中的单独实体?我希望能够分别访问“单词”字符串以及单词被视为整数的数量或次数。
我正在使用此 source 作为参考。我尝试了一些这些方法,虽然它似乎确实按数字值对数字进行排序,但给定输出:
排序的数组:[do => 1 rest => 1 look => 1通缉=> 1 give => 1 想象=> 2读=> 2授予=> 2 ever => 2爱=> 2 gong => 2 感觉=> 2表示=> 2喜欢=> 2 you => 2 live => 3写作=> 3 come => 3 知道=> 3是=> 3妈妈=> 4]
它不会将密钥和价值与彼此分开。
I am currently trying to use hashes in an array to find the keys and values of each specific item in the array. I am able to do this and both the keys and the values are separate when I haven't sorted the array, but when I create a sorted array such as:
my @sorted_pairs = %counts{$word}.sort(*.value);
It binds the values together. Is there a method for sorted hash values that allow the pairs to be split into separate entities within the array? I want to be able to access the "word" string and the count or number of times that word was seen as an integer, separately.
I am using this source as a reference. I have tried a handful of these methods and while it does seem to sort the array by numeric value given the output:
sorted array : [do => 1 rest => 1 look => 1 wanted => 1 give => 1
imagine => 2 read => 2 granted => 2 ever => 2 love => 2 gonna => 2
feel => 2 meant => 2 like => 2 you => 2 live => 3 wrote => 3 come => 3
know => 3 are => 3 mom => 4]
it doesn't separate the key and value from one another.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
raku中的单词计数
您可能需要将结果保存为(
bag
- ged) - 哈什阵列(Pairs?),然后打印出一个。
示例输入(
ishmael.txt
):使用上面的代码获得以下输出(通过指定
$_。value> = 4
)截断了以下输出。通过将第二个语句更改为
.keys.put @AOH
:或仅通过更改第二个返回。对
.values.values.put @aoh
:[注意:以上是Raku中单词计数代码的示例。它无法处理标点符号,大写等,但它是一个开始
。 https://docs.raku.org/language/hashmap#looping_over_hash_keys_and_values
Word-counting in Raku
You might want to save your results as a (
Bag
-ged) array-of-hashes (pairs?), then print out either.keys
or.values
as necessary.Sample Input (
Ishmael.txt
):Using the code above you get the following Output (full output truncated by specifying
$_.value >= 4
):And it's simple enough to return just
.keys
by changing the second statement to.keys.put for @aoh
:Or return just
.values
by changing the second statement to.values.put for @aoh
:[Note: the above is a pretty quick-and-dirty example of word-counting code in Raku. It doesn't handle punctuation, capitalization, etc., but it's a start.]
https://docs.raku.org/language/hashmap#Looping_over_hash_keys_and_values