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

  • SEARCH
  • Home
  • 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 9249759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:16:45+00:00 2026-06-18T10:16:45+00:00

In Python s= ABCC n = len(s) sorted(set([s[a:b] for a in range(n) for b

  • 0

In Python

s= "ABCC"
n = len(s)
sorted(set([s[a:b] for a in range(n) for b in range(a+1,n+2)])

gives me, alphabetically sorted sub strings with out repetitions

['A', 'AB', 'ABC', 'ABCC', 'B', 'BC', 'BCC', 'C', 'CC']

How can I further sort it by length of sub string.

['A', 'B', 'C', 'AB', 'BC', 'CC', 'ABC', 'BCC', 'ABCC']
  • 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-18T10:16:46+00:00Added an answer on June 18, 2026 at 10:16 am

    simple,

    sorted(set(s[a:b] for a in range(n) for b in range(a+1,n+1)),
           key=lambda x:(len(x),x))
    

    This creates a key by which the comparison is done. First it compares the string lengths to determine the order. If the strings have the same length, the tie-breaker is the string contents.

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

Sidebar

Related Questions

Python 2.7's argparse gives you two extension points where you can control how your
python setup.py install lipo: can't open input file: /var/tmp//ccZn3E25.out (No such file or directory)
Python beginner here. I have a text file that is sorted into columns: fields
Python works on multiple platforms and can be used for desktop and web applications,
Python's arbitrary precision decimals are lovely, but I can't seem to find a way
Python loves raising exceptions, which is usually great. But I'm facing some strings I
Python's len() and padding functions like string.ljust() are not tabstop-aware, i.e. they treat '\t'
Python 2 and 3, are the bytecode (pyo & pyc) backward compatible? can i
Python's inner/nested classes confuse me. Is there something that can't be accomplished without them?
Python 3 cleans up Python's handling of Unicode strings. I assume as part of

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.