LoginSignup
0
1

More than 5 years have passed since last update.

Python | Numpy > xs, ys: np.array() に対してxsをソートする | ysもそれに合わせて並び替える

Last updated at Posted at 2017-10-28
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0

globを使用して複数のdirectoriesからファイルを読むとき、その並びが予想通りではなかった。
そのため、処理結果のリストを並替えする必要がある。

例として、以下を考える。

xs = [3, 1, 4, 2, 5]
ys = [1, 2, 3, 4, 5]

xsでソートして、それに合わせてysも並び替えるという処理。

リンク

参考: https://stackoverflow.com/questions/9764298/is-it-possible-to-sort-two-listswhich-reference-each-other-in-the-exact-same-w

以下でできた。
(np.array()を使っているのは補間処理でnp.array()を処理するため)。

test_cosort_171028.py
import numpy as np

xs = [3, 1, 4, 2, 5]
ys = [1, 2, 3, 4, 5]

xns = np.array(xs)
yns = np.array(ys)

xns, yns = zip(*sorted(zip(xns, yns)))

print(xns)
print(yns)

run
$ python3 test_cosort_171028.py 
(1, 2, 3, 4, 5)
(2, 4, 1, 3, 5)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1