如何将序列序列变成映射,其中值是序列中的第一项的计数?

发布于 2025-02-13 00:04:58 字数 302 浏览 0 评论 0原文

我有((“通用” 7)(“ ore” 1)(“通用” 4)(“ wood” 6)(“小麦” 3)(“通用” 2)(“通用” 9)的数据(“绵羊” 5)(“砖” 8))

,我想将其变成数据

 {"brick"   1
  "generic" 4
  "sheep"   1
  "ore"     1
  "wheat"   1
  "wood"    1}

,因为“通用”在数据中出现4次,我希望键是“通用”,并且值为4因为“绵羊”出现1在数据中,我希望钥匙是“绵羊”,值为“ 1”。

I have data like (("generic" 7) ("ore" 1) ("generic" 4) ("wood" 6) ("wheat" 3) ("generic" 2) ("generic" 9) ("sheep" 5) ("brick" 8))

and I want to turn it into

 {"brick"   1
  "generic" 4
  "sheep"   1
  "ore"     1
  "wheat"   1
  "wood"    1}

Since "generic" appears 4 times in the data, I want the key to be "generic" and the value to be 4. Since "sheep" appears 1 time in the data, I want the key to be "sheep" and the value to be "1".

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

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

发布评论

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

评论(1

恰似旧人归 2025-02-20 00:04:58

使用 map

(->> '(("generic" 7) ("ore" 1) ("generic" 4) ("wood" 6) ("wheat" 3) ("generic" 2) ("generic" 9) ("sheep" 5) ("brick" 8))
     (map first)
     frequencies)

=> {"generic" 4, "ore" 1, "wood" 1, "wheat" 1, "sheep" 1, "brick" 1}

( - >>>是“ thread-last”宏。)

Use map, first and frequencies:

(->> '(("generic" 7) ("ore" 1) ("generic" 4) ("wood" 6) ("wheat" 3) ("generic" 2) ("generic" 9) ("sheep" 5) ("brick" 8))
     (map first)
     frequencies)

=> {"generic" 4, "ore" 1, "wood" 1, "wheat" 1, "sheep" 1, "brick" 1}

(->> is "thread-last" macro.)

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