1

I am new to python and I created a script to sort through "show ip accounting" information from a cisco router. The script reads a file and breaks up every line into a list, then it creates a list of every line. So I end up with a list of lists:

list a = [[192.168.0.1,172.16.0.1,3434,12222424],[192.168.2.1,172.12.0.1,33334,12667896722424]]

I want to be able to sort by the third column or 4th columns of the list within the list.

I was able to do it using a lambda function, but my question is how to duplicate this using a standard function?

here is my code below:

from sys import argv

script, option, filename = argv
a=[]
b=[]

def openfile(filename):
  file = open(filename)
  for line in file:
    if not line.startswith("  "):
      a.append((line.split()))
  return a

def sort(a,num):
  b = sorted(a, reverse=True, key=lambda x: int(x[num]))
  return b

def top5(b):
  print "Source     Destination Packets     Bytes"
  for i in b[:4]: 
    print i[0]+"    "+i[1]+"    "+i[2]+"        "+i[3]

def main(option):
  a = openfile(filename)
  if option == "--bytes":
    b = sort(a,3)
    top5(b)
  elif option == "--packets":
    b = sort(a,2)
    top5(b)
  else:
    print """
    Not a valid switch, 
    --bytes to sort by bytes 
    --packets to sort by packets."""


main(option)

So my question is how can I duplicate the lambda function as a standard custom sort function? I am trying to figure out how this works.

b = sorted(a, reverse=True, key=lambda x: int(x[num]))

1

2 Answers 2

4

how can I duplicate the lambda function as a standard custom sort function?

Do you mean this:

def sort(a, num):
  def key(x):
    return int(x[num])
  return sorted(a, reverse=True, key=key)

or perhaps this:

from functools import partial

def key(num, x):
  return int(x[num])

def sort(a, num):
  return sorted(a, reverse=True, key=partial(key, num))

?

Sign up to request clarification or add additional context in comments.

10 Comments

I guess they're actually looking for itemgetter.
@thg435: Maybe. Hard to say really. "Standard custom function" is a bit of an oxymoron.
OK, so I tried both snippets of code and they both work, My question is when you made the function key inside of the function sort, what value does it get for x, if you don't send it any values when you call it with key=key? I am trying to understand how this works,
@user2010044: x refers to the list item whose key is being requested. sorted() calls key() for every element in the list, and then uses the keys it got to do the actual sorting.
@user2010044: A key function doesn't necessarily have to be defined inside the calling function. But it you want to create a closure over a local variable inside that function, then you need to define it there (or use functools.partial, or wrap it in a local function or a lambda, etc.).
|
1

Python provides operator.itemgetter for doing this kind of thing:

def sort(a, num):
    return sorted(a, reverse=True, key=operator.itemgetter(num))

Edit

As @NPE pointed out, that doesn't convert the key to an int for sorting. For that, you're best off to stick with a lambda.

2 Comments

@NPE True. I missed the cast to int
You could do compose(int, operator.itemgetter(num)), if you had a compose function from somewhere (e.g., functional). But I don't think this is what the OP is asking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.