我有listA
和listB
大小相同。我在listA
上做GatherBy
,重新排列該列表。將相同重排應用於listB
的優雅方法是什麼?將`GatherBy`轉換爲不同的列表
例如
listA = {1, 2, 3};
listB = {a, b, c};
listA1 = GatherBy[{1, 2, 3}, OddQ];
listB1
應該成爲{{a, c}, {b}}
更新 感謝有趣的想法,我終於結束了做類似的東西貝利薩留。這讓我想起了Python的的「裝飾 - 排序 - 去除裝飾」模式
decorated = Thread[{listA, listB}];
grouped = GatherBy[decorated, OddQ[First[#]] &];
listB1 = Map[Last, grouped, {2}]
對於真正大的列表,`grouped [[All,All,-1]]`may比使用Map更快或更快(不知道這是否與之相關螞蟻爲你的情況) – 2011-02-12 21:49:08