Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

sorting - How do I sort columns of numerical file data in python

I'm trying to write a piece of code in python to graph some data from a tab separated file with numerical data.

I'm very new to Python so I would appreciate it if any help could be dumbed down a little bit.

Basically, I have this file and I would like to take two columns from it, sort them each in ascending order, and then graph those sorted columns against each other.

question from:https://stackoverflow.com/questions/65908579/how-do-i-sort-columns-of-numerical-file-data-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, you should not put code as images, since there is a functionality to insert and format here in the editor.

It's as simple as calling x.sort() and y.sort() since both of them are slices from data so that should work fine (assuming they are 1 dimensional arrays).

Here is an example:

import numpy as np
array = np.random.randint(0,100, size=50)
print(array)

Output:

[89 47 4 10 29 21 91 95 32 12 97 66 59 70 20 20 36 79 23 4]

So if we use the method mentioned before:

print(array.sort())

Output:

[ 4 4 10 12 20 20 21 23 29 32 36 47 59 66 70 79 89 91 95 97]

Easy as that :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...