将 ruby​​ 哈希值按随机顺序排列

发布于 2024-10-27 21:55:32 字数 623 浏览 5 评论 0原文

我编写了一个代码,将字母表中的每个字母与另一个字符(长度为1的字符串)相匹配,然后将这两个字符转换为散列:

Hash[String.alphabet.zip String.alphabet]
# String.alphabet returns an array with 0:A, 1:B, ... ,25:Z
# I take two times the alphabet to demonstrate

但是当我用它打印散列时,

puts "#{hash.keys.join}\n#{hash.values.join}"

它给了我

VKWLAXMBYNCZODPEQFRGSHTIUJ
VKWLAXMBYNCZODPEQFRGSHTIUJ

,虽然是正确的,但很难阅读 形式的输出

ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ

如果我想查看字母是否正确匹配,我更喜欢以[打印方法在其他哈希上正常工作并且据我所知是正确的]

那么:我如何“压缩”数组同时保持键的原始顺序?

I wrote a code that matches every letter from an alphabet with another character (string with length 1) and i turned the two into a hash:

Hash[String.alphabet.zip String.alphabet]
# String.alphabet returns an array with 0:A, 1:B, ... ,25:Z
# I take two times the alphabet to demonstrate

But when i print the hash with

puts "#{hash.keys.join}\n#{hash.values.join}"

it gives me

VKWLAXMBYNCZODPEQFRGSHTIUJ
VKWLAXMBYNCZODPEQFRGSHTIUJ

, which - while being correct - is hard to read if i wanna see if letters are matched correctly, i'd much prefer an output in the form of

ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ

[The print method works correctly on other hashes and is by my knowledge correct]

So: How can i 'zip' the arrays while keeping the original order of keys?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兮子 2024-11-03 21:55:32

关于有序哈希的答案是正确的,但在您的情况下,您不必保持哈希有序,您只需对输出进行排序即可。

puts hash.sort.transpose.map(&:join)

The answers about ordered hashes are correct, but in your case you don't necessarily have to keep the hash in order, you can just sort it for your output.

puts hash.sort.transpose.map(&:join)
我为君王 2024-11-03 21:55:32

可以在 Ruby 1.9.x 中使用的排序哈希。在 Ruby 1.8.7 中,哈希值是无序的

从 API 来看:

哈希 是键值对的集合。它类似于数组,只不过索引是通过任何对象类型的任意键而不是整数索引完成的。通过键或值遍历哈希的顺序可能看起来是任意的,并且通常不会按照插入顺序。

Sorted hashes you can use in Ruby 1.9.x. In Ruby 1.8.7 Hashes are unsorded

From API:

A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. The order in which you traverse a hash by either key or value may seem arbitrary, and will generally not be in the insertion order.

你曾走过我的故事 2024-11-03 21:55:32

问题不在于 zip,而在于 Hash

哈希表通常(在计算机科学的一般世界中)不会跟踪数据插入的顺序(或任何其他排序顺序),它想要知道的只是查找。如果您想遍历哈希表以查看其中的所有内容,您最终会按照存储元素的存储桶的顺序遍历元素。这通常是哈希函数的一些模。有了好的哈希函数,这本质上是一个随机顺序。

在 Ruby 1.8 中,哈希表就是这样实现的。

意识到人们希望他们的哈希能够记住元素的顺序,Ruby 开发人员在 Ruby 1.9 中向哈希表添加了某种链表,以便您可以以非随机顺序遍历元素。

It's not the zip, it's the Hash that's the problem.

A hash table ordinarly (in the general world of computer science) doesn't keep track of the order that data's inserted (or any other sort order) all it wants to know about is lookup. If you want to traverse a hash table to see everything that's in it, you end up traversing the elements in order of the buckets where the elements are stored. That's typically some modulo of the hash function. With a good hash function, that's essentially a random order.

In Ruby 1.8, that's how hash tables are implemented.

Realizing that people expect their Hashes to remember the order of elements, the Ruby developers added some kind of linked list to their hash tables in Ruby 1.9, so that you could traverse the elements in a non-random order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文