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 8540013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:30:11+00:00 2026-06-11T11:30:11+00:00

So I have a minor issue with a script I’m writing. I have a

  • 0

So I have a minor issue with a script I’m writing. I have a text file that looks something like:

'20 zebra 12 bear'

That’s just an example, the format is 1 line all items separated by spaces. The script works to sort them out and do a couple of other things to the strings but what I can’t figure out is how to keep it set the way it is. For example the above line should sort like this:

12
bear
20
zebra

I need to keep a number in the number place and a string in a strings place but they should be sorted alphanumerically.
Here is my script so far:

#!/usr/bin/python

# Make sure you use the proper modules.
import sys, string

# This area defines the arguments and returns a usage message should it be used incorrectly.
try:
  infilename = sys.argv[1]; outfilename = sys.argv[2]
except:
  print "Usage:",sys.argv[0], "infile outfile"; sys.exit(1)

ifile = open(infilename, 'r') # Opens the input file for reading
ofile = open(outfilename, 'w') # Opens the output file for writing
data = ifile.readlines()[0].split() # Reads the lines on the input file

# The items in the list are sorted here and defined by a space.
sort = sorted(data, key=lambda item: (int(item.partition(' ')[0])
                                   if item[0].isdigit() else float('inf'), item))

# Use this to remove any special characters in the list
filtered = [s.translate(None, string.punctuation) for s in sort]


ofile.write('\n'.join(filtered)) # Writes the final output to file (one on each line)



ifile.close() # Closes the input file
ofile.close() # Closes the output file

I know it’s not the prettiest but I haven’t been using Python long so if you have suggestions on how to make it prettier, I’m all ears. All I really need is to keep a number a number and a string a string but swap them around to sort. Thanks for any assistance given.

  • 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-11T11:30:12+00:00Added an answer on June 11, 2026 at 11:30 am

    This is a really weird question.

    def strange_sort(seq):
        """
        Sorts digitstrings (by integer value) and non-digitstrings in a
        sequence among themselves, preserving the original ds/non-ds
        signature.
        """
        numbers = iter(sorted((elem for elem in seq if elem.isdigit()), key=int))
        words = iter(sorted(elem for elem in seq if not elem.isdigit()))
        final = [next(numbers if elem.isdigit() else words) for elem in seq]
        return final
    

    which gives

    >>> strange_sort("1 2 3".split())
    ['1', '2', '3']
    >>> strange_sort("1 2 10 3".split())
    ['1', '2', '3', '10']
    >>> strange_sort("1 2 10 3 bear".split())
    ['1', '2', '3', '10', 'bear']
    >>> strange_sort("2 1 bear 10 3".split())
    ['1', '2', 'bear', '3', '10']
    >>> strange_sort("2 1 zebra 10 3 bear".split())
    ['1', '2', 'bear', '3', '10', 'zebra']
    >>> strange_sort("20 zebra 12 bear".split())
    ['12', 'bear', '20', 'zebra']
    >>> strange_sort("20 zebra 12 bear 3".split())
    ['3', 'bear', '12', 'zebra', '20']
    

    Actually, this is basically @przemo_li’s suggestion worked out.

    [edited to keep everything a string]

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

Sidebar

Related Questions

I have a minor mode. If that mode is active and the user hits
I have a custom control that needs a minor adjustment when running on XP
I have written this short script (which I've stripped away some minor detail for
I have a php script that posts links to my facebook page wall using
I have a minor issue in handling DateTime in C#. I have designed a
I have just uploaded my first app to iTunes Connect and noticed that my
This might be a minor issue, but I often have to fight with it.
Hey, SO, minor issue: I have an NSWindow I am using to report exceptions
Finally I have ended up with this. But there is a minor issue not
I have a minor issue with displaying TextViews in my app while running on

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.