A Python applet, read the two sets of numbers of each row from a text file, and output it after being sorted.

xiaoxiao2021-03-06  70

Someone in the QQ group, it is very simple, written out to play:

Text file X.txt content is

122 222 861333200056610 8613032222202103335 861303433232810005 86130113188001000 861313254618610000 8613312073654100000 8613034348185100001 8613194682965100003 8613102251300100006 8613032226257100005 8613116000937

Two numbers have a '/ t'

f = Open ('x.txt') # If your x.txt file is not in Python's path, you must use absolute path L1 = f.readLines () # This time the result of L1 is a list, each element is Each line of the file, including the stroke symbol '/n'l1=[x.split('/t') for x in l1] L1 = [[x [0], x [1] .REPLACE ('/ n', '')] for x in l1] # 这里 这里 每 的 '/ n' symbol f.close () # Close the file just opened # now L1 is a List: It is also a list The two elements of the latter store the first and second column numbers f = ('y.txt', 'W') # of the file, respectively, to store the result of the result

# At this time, I don't know why I always have an error directly. That 10005 is always ranked to the third position of the countdown. It is really strange.

L2 = [[INT (x [0]), int (x [1])] for x in l1] l2.sort () L2 = [STR (x [0]) '/ t' STR (x [ 1]) '/ n' for x in l2] f.writeLines [l2] f.close ()

The result of final Y.TXT is:

1,086,130,322,222,021,000 861313254618610000 861331207365410005 8613194682965100000 8613034348185100001 8613034332328100003 8613102251300100005 8613116000937100006 8613032226257103335 8613011318800122222 8613332000566

The result is sorted according to the numbers of the first column.

Note: If L1.Sort () is directly paired, the result is:

1,086,130,322,222,021,000 861313254618610000 8613312073654100000 8613034348185100001 8613034332328100003 8613102251300100005 8613116000937100006 8613194682965103335 8613011318800122222 861303222625710005 very strange 8613332000566

If it is in the command line, the specific steps are as follows:

>>> f = open ('x.txt') >>> L1 = f.readLines () >>> L1 ['122222 / T861332000566 / N', '10 / T8613032222202 / N ',' 103335 / T861301138800 / N ',' 1000 / T8613132546186 / N ',' 10000 / T8613312073654 / N ',' 100000 / T8613034348185 / N ',' 100001 / T8613034332328 / N ',' 10005 / T8613194682965 / N ',' 100003 / T8613102251300 / N ', '10006 / T86130322226257 / N', '100005 / T8613116000937'] >>> L1 = [x.Split ('/ t') for x in l1] >>> L1 [['122222', '8613332000566 / n'] ['10', '86130322222202 / n'], ['103335', ​​'861301131800 / n'], ['1000', '8613132546186 / n'], ['10000', '8613312073654 / N'], [ '100000', '8613034348185 / n'], ['100001', '8613034332328 / n'], ['10005', '8613194682965 / n'], ['100003', '8613102251300 / n'], ['100006 ',' 8613032226257 / n '], [' 100005 ',' 8613116000937 '] >>> f.close () >>> L1 = [[INT (x [0]), int (x [1])] for x in l1] >>> l1 [[122222, 8613332000566L], [10, 8613032222202L], [103335, 8613011318800L], [1000, 8613132546186L], [10000, 8613312073654L], [100000, 8613034348185L], [100001, 8613034332328L ], [10005, 8613194682965L], [100003, 8613102251300L], [100006, 8613032226257L], [100005, 8613116000937L]] >>> l1.sort () >>> l1 [[10, 8613032222202L], [1000, 8613132546186L], [10000, 8613312073654L], [10005, 8613194682965L], [100000, 8613034348185L ], [100001, 8613034332328L], [100003, 8613102251300L], [100006, 8613032226257L], [103335, 8613011318800L], [122222, 8613332000566L] >>> f =

转载请注明原文地址:https://www.9cbs.com/read-120940.html

New Post(0)