Joh*_*ohn 5 wolfram-mathematica
我怎样才能获得选择的索引而不是值。IE
list={3->4, 5->2, 1->1, 5->8, 3->2};
Select[list, #[[1]]==5&]; (* returns {5->2, 5->8} *)
Run Code Online (Sandbox Code Playgroud)
我想要类似的东西
SelectIndices[list, #[[1]]==5&]; (* returns {2, 4} *)
Run Code Online (Sandbox Code Playgroud)
编辑:我找到了上面直接问题的答案(见下文),但是排序怎么样。假设我想对列表进行排序,但不是返回排序后的列表,而是想按排序后的列表的顺序返回索引?
好吧,我想出了一个方法来做到这一点。Mathematica 使用如此不同的词汇,搜索文档对我来说通常仍然没有成果(我一直在搜索“Mathematica Select 中的元素索引”之类的内容,但没有结果。)
无论如何,这似乎是执行此操作的方法:
Position[list, 5->_];
Run Code Online (Sandbox Code Playgroud)
我想是时候阅读 Mathematica 中的模式了。
WRT 编辑后剩下的问题:订购怎么样?
In[26]:= Ordering[{c, x, b, z, h}]
Out[26]= {3, 1, 5, 2, 4}
In[28]:= {c, x, b, z, h}[[Ordering[{c, x, b, z, h}]]]
Out[28]= {b, c, h, x, z}
In[27]:= Sort[{c, x, b, z, h}]
Out[27]= {b, c, h, x, z}
Run Code Online (Sandbox Code Playgroud)