Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:50:27+00:00 2026-06-17T21:50:27+00:00

I am new to python and I created a script to sort through show

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:50:28+00:00Added an answer on June 17, 2026 at 9:50 pm

    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))
    

    ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I'm completely new to this. I created a python script that imports tkinter
I'm really new to Python and Django. I created a class in Python that
I'm creating a game, and am quite new to Python generally. I created a
I'm a new Python programmer who is making the leap from 2.6.4 to 3.1.1.
I'm new to python developing. I'm trying to write something in a file (tmp.addr2line)
Caveat: new to Python. Wanting to hear from professionals who actually use it: What
I am new to python and have been working through the examples in Swaroop
I have created a python script to try and make my life as a
This is my python script which creates a test.txt file when executed. when I
So basically, I'm a bit new to Python. I created a server and have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.