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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:28:38+00:00 2026-06-09T00:28:38+00:00

I have two lists which have been created by .csv files. The first consists

  • 0

I have two lists which have been created by .csv files. The first consists of a branch id number and a list of corresponding flows. The second is the order in which I wish to have the branche ids and their corrsponding flows sorted. They are as follows:

branch_flows = [['1234-2321-1', [55, 76, 3, 55, 6]],
                ['1546-2645-1', [4, 6, 56, 3, 4]],
                // ...
                ['4123-1234-1', [6, 12, -4, 7, 9]]
               ]

and

ordered_branches = ['1234-2321-1',
                    '1234-4123-1',
                    // ...
                    '1546-2645-1']

I am wondering how to sort branch_flows the same way ordered_branches is ordered, but for the flows to stay related to the same ids after sorting? The main difficulty being that some of the branch ids in branch_flows first two parts are reversed, but I need them to be sorted as if they were not.

e.g. looking to the lists above, desired output would be having branch_flows sorted in a way that the final list in branch_flows was placed second in the sorted list (as 1234-4123-1 in ordered_branches can equal both 1234-4123-1 AND 4123-1234-1 in branch_list, as the order in branch_flows can sometimes be the reverse of that in ordered_branches).

I originally tried to use dictionarys as look up tables but ran into trouble with the reading reverse order part. Help much appreciated!

  • 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-09T00:28:39+00:00Added an answer on June 9, 2026 at 12:28 am

    You need to construct an appropriate key function to the Python sort function.

    Ignoring the reversed-order issue, it’s quite easy:

    def key(branch):
        id, flows = branch
        return ordered_branches.index(id)
    

    Considering the reversed-order issue, we can use:

    def key(branch):
        id, flows = branch
        try:
            return ordered_branches.index(id)
        except ValueError:
            parts = id.split('-')
            id = '-'.join((parts[1], parts[0], parts[2]))
            return ordered_branches.index(id)
    

    Now you can sort branch_flows as sorted(branch_flows, key=key).


    You can speed this up by turning ordered_branches into a dictionary:

    order_dict = dict((x, i) for i, x in enumerate(ordered_branches))
    

    and instead of ordered_branches.index(id) use order_dict[id] (also change ValueError to KeyError).


    As a time-space tradeoff you could construct the reversed-order ids in the dict:

    def reverse_id(id):
        parts = id.split('-')
        return '-'.join((parts[1], parts[0], parts[2]))
    order_dict = dict((x, i) for i, x in enumerate(ordered_branches))
    order_dict.update((reverse_id(x), i) for x, i in order_dict.items())
    

    Now your key function just looks like:

    def key(branch):
        id, flows = branch
        return order_dict[id]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I have two lists, the first is items which contains around 250 tuples,
I have two files which both contain a list of words. Is there an
I have two lists which are guaranteed to be the same length. I want
I've got two long lists A and B which have the same length but
I have an ordered list which I have styled in two ways: Coloured the
I have two polygons which are defined by a list of points: x1,y1; x2,y2;
I have a method which compares two objects and returns a list of all
I have a list of items, which has two values, a name and an
I have one drop down list in my page, which contains two options. What
I have two methods using the EF4 - one which returns a list 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.