在 Mathematica 中重新排序列表元素的更好方法

发布于 2024-12-23 00:43:26 字数 187 浏览 1 评论 0原文

我想根据每个元素的第一个组件按 lst = {"3", "1", “2”,“9”}。也就是说,我希望输出为 {{{"3", 2}}, {{"1", 2}}, {{"2", 2}}, {{"9", 2 }}}

目前我有一种丑陋的解决方案,但想知道是否有人可以帮助提供一个简洁的解决方案。多谢。

I want to re-order the result of Tally[Characters["2723198931"]] according to its every element's first component in the order of lst = {"3", "1", "2", "9"}. That is, I want the output to be {{{"3", 2}}, {{"1", 2}}, {{"2", 2}}, {{"9", 2}}}.

Currently I have a sort of ugly solution, but wonder if anybody can help to give a succinct solution. Thanks a lot.

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

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

发布评论

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

评论(3

赏烟花じ飞满天 2024-12-30 00:43:26

一种方法可能是

data = Tally[Characters["2723198931"]];
lst = {"3", "1", "2", "9"};

(*algorithm*)

pos =  Position[data[[All, 1]], #] & /@ lst;
Extract[data, pos]

输出

{{{"3", 2}}, {{"1", 2}}, {{"2", 2}}, {{"9", 2}}}

上面的更新 2:37 am

屏幕截图,我在 Windows 7 上使用 v 8.04

在此输入图像描述

one way may be

data = Tally[Characters["2723198931"]];
lst = {"3", "1", "2", "9"};

(*algorithm*)

pos =  Position[data[[All, 1]], #] & /@ lst;
Extract[data, pos]

output

{{{"3", 2}}, {{"1", 2}}, {{"2", 2}}, {{"9", 2}}}

Update 2:37 am

screen shot of the above, I am using v 8.04 on windows 7

enter image description here

没有伤那来痛 2024-12-30 00:43:26

这应该是相当快的,但我没有测试过。在长列表中,您可以使用 Dispatch[rls] 获得更好的性能。

key = {"3", "1", "2", "9"};
tal = Tally[Characters["2723198931"]];

rls = #[[1]] -> # & /@ tal;
key /. rls
{{"3", 2}, {"1", 2}, {"2", 2}, {"9", 2}}

或者,您可以使用它,这对于长 Tally 列表来说速度要快一些:

rls = Thread[tal[[All, 1]] -> tal];

This should be quite fast, but I didn't test it. On long lists you may get better performance using Dispatch[rls].

key = {"3", "1", "2", "9"};
tal = Tally[Characters["2723198931"]];

rls = #[[1]] -> # & /@ tal;
key /. rls
{{"3", 2}, {"1", 2}, {"2", 2}, {"9", 2}}

Alternatively you could use this, which is little faster for long Tally lists:

rls = Thread[tal[[All, 1]] -> tal];
一片旧的回忆 2024-12-30 00:43:26

或者,您可以执行类似的操作,

With[{tal = Tally[Characters["2723198931"]]},
  Flatten@Pick[tal, tal[[All, 1]], #] & /@ key] 

您是否总是计算字符串中的字符?如果是这样,直接计算字符数可能比先将其转换为字符列表更有效。例如考虑

str = StringJoin[RandomChoice[CharacterRange["0", "9"], 1000]];
key = {"3", "1", "2", "9"};

({#, StringCount[str, #]} & /@ key) // Timing

(* output: {0.000121, {{"3", 98}, {"1", 112}, {"2", 99}, {"9", 107}}} *)

With[{tal = Tally[Characters[str]]},
  Flatten@Pick[tal, tal[[All, 1]], #] & /@ key] // Timing 

(* output: {0.000567, {{"3", 98}, {"1", 112}, {"2", 99}, {"9", 107}}} *)

Alternatively, you could do something like

With[{tal = Tally[Characters["2723198931"]]},
  Flatten@Pick[tal, tal[[All, 1]], #] & /@ key] 

Are you always counting characters in a string? If so it might be more efficient to count the characters directly instead of transforming it to a list of characters first. Consider for example

str = StringJoin[RandomChoice[CharacterRange["0", "9"], 1000]];
key = {"3", "1", "2", "9"};

({#, StringCount[str, #]} & /@ key) // Timing

(* output: {0.000121, {{"3", 98}, {"1", 112}, {"2", 99}, {"9", 107}}} *)

With[{tal = Tally[Characters[str]]},
  Flatten@Pick[tal, tal[[All, 1]], #] & /@ key] // Timing 

(* output: {0.000567, {{"3", 98}, {"1", 112}, {"2", 99}, {"9", 107}}} *)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文