简体   繁体   English

如何对数值字典列表进行排序?

[英]How do I sort list of numerical value dictionaries?

I have two files with 6000 numerical values.I am giving just first 15 values of both. 我有两个具有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 new2.txt

    0
  100
  200
    1
  101
  201
    2
  102
  202
    3
  103
  203
    4
  104
  204

I want to create a new list from the base.txt values(velocities) that would correspond to ascending order of second file.(0,1,2,3,4,5,..) My code so far 我想从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))

My idea was to read files as lists,create dictionary and then try to sort out the values,but this code is not working. 我的想法是将文件读取为列表,创建字典,然后尝试对值进行排序,但是此代码不起作用。 I got this 我懂了

<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

The files are here http://pastebin.com/he1RuSnv 这些文件在这里http://pastebin.com/he1RuSnv

http://pastebin.com/VfXZB4W3 http://pastebin.com/VfXZB4W3

When I try solution of CPanda,I got 当我尝试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

This is not what I want,Iwant second index to go 0,1,2,3,4,5 and so on... 这不是我想要的,想要第二个索引去0、1、2、3、4、5等...

To solve the error, your last line, the one with sorted should be 要解决该错误,您的最后一行应该是已sorted

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

Sorted returns a list of key value tuples from the dict. Sorted从字典中返回键值元组的列表。

Then with the list comprehension you extract the ordered values into your newlist 然后用列表解析您提取有序值到您的newlist

You can also merge the last two lines into one: 您还可以将最后两行合并为一个:

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

Using a dictionary or an associative array in this case is not what you are looking for. 在这种情况下,使用词典或关联数组不是您想要的。 A dictionary is orderless and should not be used when order matters. 字典是无序的,当顺序很重要时不应该使用字典。

I would use a different data structure like the one mentioned here. 我将使用一种不同的数据结构,如此处所述。

Sort a Python dictionary by value 按值对Python字典排序

Try this 尝试这个

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
  • Use generator expressions instead of lists for memory efficiency and speed. 使用生成器表达式而不是列表来提高内存效率和速度。
  • Use context managers to automatically close open files 使用上下文管理器自动close打开的文件

Please comment if it doesn't work for you. 如果对您不起作用,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM