我有兩個6000數值的文件,我只給出了前15個值。如何對數值字典列表進行排序?
base.txt
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
2.900000e+03
new2.txt
0
100
200
1
101
201
2
102
202
3
103
203
4
104
204
我想創建,將對應於第二個文件的升序base.txt值(速度)的新列表。(0,1,2, 3,4,5 ......) 到目前爲止我的代碼
import itertools
from operator import itemgetter
vel = [line.strip() for line in open("base.txt", 'r')]
ind = [line.strip() for line in open("new2.txt", 'r')]
print type(vel)
print type(ind)
adict = dict(itertools.izip(ind,vel))
newlist = sorted(adict, key=itemgetter(ind))
我的想法是讀取文件的列表,創建字典,然後嘗試理清值,但是這個代碼不工作。 我得到這個
<type 'list'>
<type 'list'>
Traceback (most recent call last):
File "m1.py", line 11, in <module>
newlist = sorted(adict, key=itemgetter(ind))
TypeError: string indices must be integers, not list
的文件是在這裏 http://pastebin.com/he1RuSnv
當我嘗試CPanda的解決方案,我
2.900000e+03 0
2.900000e+03 1
2.900000e+03 10
2.900000e+03 100
2.900000e+03 1000
2.900000e+03 1001
2.900000e+03 1002
2.900000e+03 1003
2.900000e+03 1004
2.900000e+03 1005
2.900000e+03 1006
2.900000e+03 1007
2.900000e+03 1008
2.900000e+03 1009
2.900000e+03 101
2.900000e+03 1010
2.900000e+03 1011
2.900000e+03 1012
這不是我想要的,Iwant第二個索引去0,1,2,3,4,5等等......
爲什麼不工作?預期的輸出是什麼,你會得到什麼,即任何錯誤? – Francesco
@Francesco已編輯我的帖子,看看! –