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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:07:58+00:00 2026-06-14T14:07:58+00:00

I’m using Python 2.6.2. I have a list of tuples pair which I like

  • 0

I’m using Python 2.6.2. I have a list of tuples pair which I like to sort using two nested conditions.

  1. The tuples are first sorted in descending count order of fwd_count,
  2. If the value of count is the same for more than one tuple in fwd_count, only those tuples having equal count need to be sorted in descending order based on values in rvs_count.
  3. The order does not matter and the positioning can be ignored, if
    a) tuples have the same count in fwd_count and also in rvs_count, or
    a) tuples have the same count in fwd_count and does not exist in rvs_count

I managed to write the following code:

pair=[((0, 12), (0, 36)), ((1, 12), (0, 36)), ((2, 12), (1, 36)), ((3, 12), (1, 36)), ((1, 36), (4, 12)), ((0, 36), (5, 12)), ((1, 36), (6, 12))]

fwd_count = {}
rvs_count = {}

for link in sorted(pair):  
    fwd_count[link[0]] = 0
    rvs_count[link[1]] = 0

for link in sorted(pair):  
    fwd_count[link[0]] += 1
    rvs_count[link[1]] += 1

#fwd_count {(6, 12): 1, (5, 12): 1, (4, 12): 1, (1, 36): 2, (0, 36): 2}
#rvs_count {(3, 12): 1, (1, 12): 1, (1, 36): 2, (0, 12): 1, (2, 12): 1, (0, 36): 1}

fwd_count_sort=sorted(fwd_count.items(), key=lambda x: x[1], reverse=True)
rvs_count_sort=sorted(rvs_count.items(), key=lambda x: x[1])

#fwd_count_sort [((1, 36), 2), ((0, 36), 2), ((6, 12), 1), ((5, 12), 1), ((4, 12), 1)]
#rvs_count_sort [((3, 12), 1), ((1, 12), 1), ((1, 36), 2), ((0, 12), 1), ((2, 12), 1), ((0, 36), 1)]

The result I am looking for is:

#fwd_count_sort_final [((0, 36), 2), ((1, 36), 2), ((6, 12), 1), ((5, 12), 1), ((4, 12), 1)]

Where the position of (1, 36) and (0, 36) have swapped position from the one in fwd_count_sort.

Question:

  1. Is there a better way to do multi condition sorting using fwd_count and rvs_count information at the same time? (Only the tuples are important, the sort value need not be recorded.), or
  2. Would I need to sort it individually for each conditions (as I did above) and try to find mean to integrate it to get the result I wanted?

I am currently working on item #2 above, but trying to learn if there are any simpler method.

This is the closest I can get to what I am looking for “Bidirectional Sorting with Numeric Values” at http://stygianvision.net/updates/python-sort-list-object-dictionary-multiple-key/ but not sure I can use that if I create a new dictionary with {tuple: {fwd_count : rvs_count}} relationship.

Update: 12 November 2012 — SOLVED

I have managed to solve this by using list. The below are the codes, hope it is useful for those whom are working to sort multi condition list.

#pair=[((0, 12), (0, 36)), ((1, 12), (1, 36)), ((2, 12), (0, 36)), ((3, 12), (1, 36)), ((1, 36), (4, 12)), ((0, 36), (5, 12)), ((1, 36), (6, 12))]

rvs_count = {}
fwd_count = {}

for link in sorted(pair):
  rvs_count[link[0]] = 0
  fwd_count[link[1]] = 0

for link in sorted(pair):
  rvs_count[link[0]] += 1
  fwd_count[link[1]] += 1

keys = []
for link in pair:
    if link[0] not in keys:
        keys.append(link[0])
    if link[1] not in keys:
        keys.append(link[1])

aggregated = []
for k in keys:
    a = -1
    d = -1
    if k in fwd_count.keys():
        a = fwd_count[k]
    if k in rvs_count.keys():
        d = rvs_count[k]
    aggregated.append(tuple((k, tuple((a,d)) )))

def compare(x,y):
    a1 = x[1][0]
    d1 = x[1][1]
    a2 = y[1][0]
    d2 = y[1][1]
    if a1 > a2:
        return  - a1 + a2
    elif a1 == a2:
        if d1 > d2:
            return d1 - d2
        elif d1 == d2:
            return 0
        else:
            return d1 - d2
    else:
        return - a1 + a2

s = sorted(aggregated, cmp=compare)
print(s)

j = [v[0] for v in s]
print(j)

Thanks to Andre Fernandes, Brian and Duke for giving your comments on my work

  • 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-14T14:07:59+00:00Added an answer on June 14, 2026 at 2:07 pm

    If you require to swap all first (of pair) elements (and not just (1, 36) and (0, 36)), you can do

    fwd_count_sort=sorted(rvs_count.items(), key=lambda x: (x[0][1],-x[0][0]), reverse=True)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.