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

标签 wolfram-mathematica

我想重新排序 Tally[Characters["2723198931"]] 的结果根据其每个元素的第一个分量,按 lst = {"3", "1", "2", "9"} 的顺序排列.也就是说,我希望输出为 {{{"3", 2}}, {{"1", 2}}, {{"2", 2}}, {{"9", 2}}} .

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

最佳答案

一种方式可能是

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

enter image description here

关于wolfram-mathematica - 在 Mathematica 中重新排序列表元素的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8633109/

相关文章:

excel - 分析循环测试数据的最佳程序

python - 使用 subprocess.Popen 将输出从 Mathematica 脚本重定向到 PIPE 而不是 stdout 会产生空字符串

wolfram-mathematica - 在不显式迭代的情况下找到满足某些条件的最大矩形 block

csv - 数学: how can I model RC and get the R and C value from CSV

wolfram-mathematica - 为什么这个表达式替换不起作用?

wolfram-mathematica - 在 Mathematica 中绘制图例

image - 如何有效地操作大量图像的像素值?

java - 在 Java 中获取 Mathematica 图像

wolfram-mathematica - 如何在 Mathematica 中以惯用的方式用列表中的前一个元素替换每个 0?

wolfram-mathematica - 如何独立于其他人对齐网格中的第一列和第一行?