0

So I got this text file looking like this:

 PID     TTY              TIME          CMD
1000    pts/2           00:00:00        aash
9000    pts/2           00:00:00        bash
3000    pts/2           00:00:00        cash

What I want to end up with is some kind of dictionary where I save |(PID,CMD)| sorted by PID descending.

So it would look like this:

[(9000,bash),(3000,cash),(1000,aash)]

Any Ideas? This is how I read the file and save in dictionary.

dict = {}

with open('newfile.txt') as f:
     next(f)   #skipping first line
     for line in f:
        result[line.split()[3]] = int(line.split()[0])

Appreciate any kind of help! Thanks in advance !

dummker
  • 315
  • 1
  • 13
  • 1
    If you need something sorted, you probably don't want to use a `dict`. Why do you need it sorted? What are you doing with the sorted data afterwards? – A. Sokol Jan 14 '17 at 18:38
  • Sorry I forgot to mention: i got my finals next week and this is just some kind of exercise my Professor gave us. – dummker Jan 14 '17 at 18:39
  • Well, you can't sort a dictionary, so I think you need to change your question or add some details before anyone can really give you advice. Maybe make your own goal for the data, like printing out a sorted list. – A. Sokol Jan 14 '17 at 18:41
  • Possible duplicate of [Reorder dictionary in python according to a list of values](http://stackoverflow.com/questions/5925731/reorder-dictionary-in-python-according-to-a-list-of-values) – sco1 Jan 14 '17 at 19:20
  • That doesn't really matter. In the end I just need it printed out in sorted order. That was just the only data structure I could think of to save keys together with values. Sorry I'm not very experienced in python! – dummker Jan 14 '17 at 19:34

2 Answers2

1

So this is the solution:

import collections

result = {}

with open('newfile.txt') as f:
    next(f)
    for line in f:
        result[line.split()[3]] = int(line.split()[0])

print(collections.OrderedDict(sorted(result.items(), key=lambda t: t[1])))

This is what it prints out:

OrderedDict([('aash', 1000), ('cash', 3000), ('bash', 9000)])])
dummker
  • 315
  • 1
  • 13
  • 1
    You are calling _split_ on the same line twice. This may be serious performance issue in real-life app – volcano Jan 14 '17 at 19:44
1

If you need to end up with a list, then best is to read the data into a list and then to sort it, here is how:

lst = []
with open('newfile.txt') as f:
    next(f)
    for line in f:
        if line.split() != '':  # watch out for empty lines
            a, b, c, d = line.split()
            lst.append((int(a), d))

lst = sorted(lst)
print(lst)

====
[(1000, 'aash'), (3000, 'cash'), (9000, 'bash')]

sorted() sorts by the first item on the tuple, so you can use it in its basic form.

If what you need is a dictionary where the keys are sorted, then you can use OrderedDict, just import it and add another line to the code:

from collections import OrderedDict

and then

d = OrderedDict(lst)
print(d)

And here is the result:

OrderedDict([(1000, 'aash'), (3000, 'cash'), (9000, 'bash')])
hansaplast
  • 11,007
  • 2
  • 61
  • 75
Israel Unterman
  • 13,158
  • 4
  • 28
  • 35