gpt4 book ai didi

ruby - 数组数组 - 如何选择唯一元素?

转载 作者:太空宇宙 更新时间:2023-11-03 16:04:25 25 4
gpt4 key购买 nike

我有一个“Q&A”数组,每个元素都是三个元素(Q、A 和 Animal)的子数组。如何选择所有独特的动物?

我可以单独选择动物:

[@q_and_a[0][2]] + [@q_and_a[1][2]] + [@q_and_a[2][2]] +[@q_and_a[3][2]] 
# => ["Elephant", "Elephant", "Spider", "Spider"]

最佳答案

使用 map uniq 功能

@q_and_a.map { |a| a[2] }.uniq

关于ruby - 数组数组 - 如何选择唯一元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17493794/

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