2016-04-26 147 views
3

我有两个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

http://pastebin.com/VfXZB4W3

当我尝试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等等......

+0

为什么不工作?预期的输出是什么,你会得到什么,即任何错误? – Francesco

+0

@Francesco已编辑我的帖子,看看! –

回答

2

为了解决这个错误,你的最后一行,在一个与sorted

newlist = [el[1] for el in sorted(adict.items())] 

排序返回从字典键值元组的列表。

然后用列表解析您提取有序值到您newlist

您还可以在最后两行合并为一个:

newlist = [el[1] for el in sorted(itertools.izip(ind,vel))] 
1

试试这个

import itertools 

with open("base.txt") as fv, open("new2.txt", 'r') as fi: 
    vel = (line.strip() for line in fv) 
    ind = (int(line.strip()) for line in fi) 
    z = itertools.izip(ind, vel) # sort according to ind 
    # itertools.izip(vel, ind) # sort according to vel 
    for i, v in sorted(z): 
     print v,i 

# interactive session 
l1 = ['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'] # list(vel) 
l2 = [0, 100, 200, 1, 101, 201, 2, 102, 202, 3, 103, 203, 4, 104, 204] # list(ind) 
# result 
2.900000e+03 0 
2.900000e+03 1 
2.900000e+03 2 
2.900000e+03 3 
2.900000e+03 4 
2.900000e+03 100 
2.900000e+03 101 
2.900000e+03 102 
2.900000e+03 103 
2.900000e+03 104 
2.900000e+03 200 
2.900000e+03 201 
2.900000e+03 202 
2.900000e+03 203 
2.900000e+03 204 
  • 使用生成器表达式而不是列表来提高内存效率和速度。
  • 使用上下文管理器自动close打开的文件

请评论,如果它不为你工作。

+1

@C熊猫没有这不工作。我会粘贴我的文件,所以你可以尝试自己。 –

+0

你想排序第二个,并根据你想安排的第一个,对不对? –

+0

是的,第二个应该从0开始然后升序,我想安排第一个。 –

相关问题