在 Mathematica 中重新排序列表元素的更好方法
我想根据每个元素的第一个组件按 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种方法可能是
输出
上面的更新 2:37 am
屏幕截图,我在 Windows 7 上使用 v 8.04
one way may be
output
Update 2:37 am
screen shot of the above, I am using v 8.04 on windows 7
这应该是相当快的,但我没有测试过。在长列表中,您可以使用
Dispatch[rls]
获得更好的性能。或者,您可以使用它,这对于长 Tally 列表来说速度要快一些:
This should be quite fast, but I didn't test it. On long lists you may get better performance using
Dispatch[rls]
.Alternatively you could use this, which is little faster for long Tally lists:
或者,您可以执行类似的操作,
您是否总是计算字符串中的字符?如果是这样,直接计算字符数可能比先将其转换为字符列表更有效。例如考虑
Alternatively, you could do something like
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