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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:18:40+00:00 2026-06-07T10:18:40+00:00

I have an algorithm that generates a list containing an unknown number of sublists,

  • 0

I have an algorithm that generates a list containing an unknown number of sublists, with each sublist having an unknown number of string elements as well as one floating point number. I need these sublists sorted inside of the main list according to this float. Also, the order of the strings in the sublists are not be be rearranged.

I currently have this running through a chunk of code (linked below) that sorts it using a dict. I am getting KeyErrors as you can see. I haven’t worked with dicts before, so im not sure where to go from here. If there is a better way then a dict im open to that too. http://ideone.com/wr8UA

The floats are not sequential, im not sure how big of a deal this is using the method i have. Meaning, numbers will occasionally be skipped (in the example list in Ideone they are 1.0, 2.0, 4.0; skipping 3.0 to simulate this situation at runtime). They also arent in any specific location in the sublists, hence all the for loops searching for them.

Hopefully this is clear, i tried asking it before and got downvotes instead of questions about what was confusing to people. Let me know if something is amiss. Thanks everyone!

Edit: Code in the body as requested:

listed = [ ["1 NWZ", "1 RWZ", "2 NWZ", "2 RWZ", "1E HZ", "1W HZ", "1-2EHZ", 2.0, "2W HZ"],
["1 NWZ", "1W HZ", "3E FZ", "SNOK", "POK", 3.0, "1-2EHZ", "2E AK", "2W HZ"],
["1 BW", "1AW AS", "3E FZ", "1BWAK", "POK", "TESTK", "1-2EHZ", "2E AK", 1.0]] 

dictionary={}
for sub_list in listed:
    for value in sub_list:
        if isinstance(value,float):
            dictionary[str(value)]=sub_list
        else:
            pass
ordered_list=[]
    for i in range(1,len(listed)+1):
    if dictionary[str(i)]:
        ordered_list.append(dictionary[str(i)])

for sub_list in ordered_list:
    print sub_list
  • 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-07T10:18:41+00:00Added an answer on June 7, 2026 at 10:18 am

    The sort method has a handy key keyword argument, that let’s you specify a function to call to determine on what information a list should be sorted.

    Sorting your lists is as easy as writing a function that returns the float value contained in each sub list:

    def sortOnFloat(sublist):
        return [v for v in sublist if isinstance(v, float)]
    

    Note that I simply return a list of all the float values; much easier than having to return just one. This’ll work even if there are 0 float values in the sublist.

    Sort your list like this:

    listed.sort(key=sortOnFloat)
    

    I’ve cloned your example and updated it with the above method: http://ideone.com/u8ufK

    Output generated:

    ['1 BW', '1AW AS', '3E FZ', '1BWAK', 'POK', 'TESTK', '1-2EHZ', '2E AK', 1.0]
    ['1 NWZ', '1 RWZ', '2 NWZ', '2 RWZ', '1E HZ', '1W HZ', '1-2EHZ', 2.0, '2W HZ']
    ['1 NWZ', '1W HZ', '3E FZ', 'SNOK', 'POK', 3.0, '1-2EHZ', '2E AK', '2W HZ']
    

    Note that the .sort() method sorts the list in-place. You can also use the sorted() function to generate a new list that has been sorted; it takes the same argument:

    orderedlist = sorted(listed, key=sortOnFloat)
    

    but note that in-place sorting is always faster.

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

Sidebar

Related Questions

I have an algorithm that generates strings based on a list of input words.
I have simple algorithm that clean the whitespace from half string until end. Here
I have written an algorithm that takes geospatial data and performs a number of
I have an array / list of numbers. Each number has a certain priority
i have an algorithm that generates permutations of a given word. I'm trying to
I have an algorithm that finds the minimum of function/n on Rn. And I
I have an algorithm that finds a value in a cell, for this case
I have an algorithm that recursively makes change in the following manner: public static
I have this algorithm that I want to implement on VB6. Sub Main() dim
If I have an algorithm that takes n log n steps (e.g. heapsort), where

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.