Created
September 13, 2018 11:08
-
-
Save good-old-times/2029608529ae64bb67603d49465744c2 to your computer and use it in GitHub Desktop.
The teacher asks me for this code. The sorting of words in somefile using array, for, sorted(), open()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
data = [] | |
datalen = 0 | |
with open(sys.argv[1],'r') as f: | |
for line in f: | |
for word in line.split(): | |
datalen=datalen+1 | |
data.insert(datalen, " "+word) | |
print("Array data: (NO SORTING)") | |
print(data) | |
data=sorted(data) | |
print("SORTED:") | |
print(data) | |
open(sys.argv[1],'r+').writelines(data) | |
# for execute for example: python __main__.py data.txt | |
# data.txt: 1 2 3 4 4 5 6 7 8 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment