【问题标题】:Neat extraction of elements in nested lists (Mathematica)整齐地提取嵌套列表中的元素(Mathematica)
【发布时间】:2014-08-27 15:13:52
【问题描述】:

这是一个关于在 Mathematica 中重新排列嵌套列表部分的问题。
我有一个嵌套列表:
DatenList = {{1, {100, 200}}, {2, {101, 201}}, {3, {102, 202}}};
并想获得另一个列表,例如
{{1,100},{2,101}}

有没有比
temp = DatenList[[1 ;; 2, 1]]; temp2 = DatenList[[1 ;; 2, 2]][[;; , 1]]; temp = {temp}~Join~{temp2}; finalList = Transpose[temp]更简洁的方法
产生
{{1, 100}, {2, 101}}

【问题讨论】:

    标签: list nested wolfram-mathematica extract


    【解决方案1】:
    temp2 = DatenList[[1 ;; 2, 2]][[;; , 1]]
    

    可以写成更短的

    temp2 = DatenList[[1 ;; 2, 2, 1]]
    

    否则,整个操作可以通过几种方式完成:-

    finalList = {#1, First[#2]} & @@@ DatenList[[1 ;; 2]]
    
    finalList = DatenList[[1 ;; 2]] /. {a_Integer, {b_, _}} :> {a, b}
    
    finalList = Replace[DatenList[[1 ;; 2]], {a_, {b_, _}} :> {a, b}, 1]
    
    finalList = MapThread[{#1, First[#2]} &, Transpose[DatenList[[1 ;; 2]]]]
    

    【讨论】:

    • 感谢您的快速回答。我最喜欢第一个解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多