我有一个numpy数组-
mapPartitionsWithInputSplit(new Function2<InputSplit, Iterator<Tuple2<LongWritable, Text>>, Iterator<Tuple2<String,String>> >(){
@Override
public Iterator<Tuple2<String,String>> call(InputSplit arg0,
Iterator<Tuple2<LongWritable, Text>> dataIterator) throws Exception {
FileSplit fileSplit = (FileSplit) arg0;
//Retrieve the file name from the split
String fileLocation = fileSplit.getPath().toString();
List<Tuple2<String,String>> retList = new LinkedList<Tuple2<String,String>>();
while(dataIterator.hasNext())
{
String data = dataIterator.next()._2.toString();
retList.add(new Tuple2<String,String>(fileLocation,data));
}
return retList.iterator();
}
},true)
,并希望按第1列然后按第0列对其进行排序,同时保留行顺序。我设法按轴1对其进行了排序-
a = np.array([[1,23],[5,4],[56,12], [7,29], [3,4], [2,15],[2,8], [4,8]])
但第0列也必须在第1列的升序内按升序排列,例如:
a = a[a[:,1].argsort()]
array([[ 5, 4],
[ 3, 4],
[ 4, 8],
[ 2, 8],
[56, 12],
[ 2, 15],
[ 1, 23],
[ 7, 29]])
如何实现?
答案 0 :(得分:1)
尝试通过多个键使用sorted
:
a = np.array([[1,23],[5,4],[56,12], [7,29], [3,4], [2,15],[2,8], [4,8]])
>>> np.array(sorted(a, key=lambda x: (x[1], x[0]))) # sort col 1 then col 0.
array([[ 3, 4],
[ 5, 4],
[ 2, 8],
[ 4, 8],
[56, 12],
[ 2, 15],
[ 1, 23],
[ 7, 29]])