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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:53:42+00:00 2026-05-22T01:53:42+00:00

I have two lists of fixed-length tuples. This function calculates a fraction (ratio) for

  • 0

I have two lists of fixed-length tuples. This function calculates a fraction (ratio) for respective elements (note, fX() does not use comprehension for readability here).

>>> def fX(a,b):  
>>>    c=[]  
>>>    for i in range(len(a)):  
>>>        c.append([a[i][x]/float(a[i][x]+b[i][x]) for x in range(len(a[i]))])
>>>    return c  

When all values are non-zero, fX() works:

>>> a[0]=(3, 4, 17, 9.6667, 6.6583, 0.4310, 1)  
>>> b[0]=(4, 4, 12, 8.0, 3.2660, 0.0002, 1)  
>>> fX(a,b)  
>>> [[0.4286, 0.5, 0.5862, 0.5472, 0.6710, 0.9995, 0.5]]  

However, when any pair’s values sum to zeros, fX() fails:

>>> a[0]=(3, 4, 17, 9.6667, 6.6583, 0.4310, 0)  
>>> b[0]=(4, 4, 12, 8.0, 3.2660, 0.0002, 0)  
>>> fX(a,b)  
Traceback (most recent call last):  
  File "<pyshell#59>", line 1, in <module>  
    fX(a,b)  
  File "<pyshell#52>", line 4, in fX  
    c.append([a[i][x]/float(a[i][x]+b[i][x]) for x in range(len(a[i]))])  
ZeroDivisionError: float division  

I’m need a function, fY(), that gives this desired outcome without resorting to a brute force test of each value:

>>> a[i]=(3, 4, 17, 9.6667, 6.6583, 0.4310, 0)  
>>> b[i]=(4, 4, 12, 8.0, 3.2660, 0.0002, 0)  
>>> fY()  
>>> [[0.4286, 0.5, 0.5862, 0.5472, 0.6710, 0.9995, 0.0]]  

Thanks.

  • 1 1 Answer
  • 1 View
  • 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-05-22T01:53:43+00:00Added an answer on May 22, 2026 at 1:53 am

    Use the a if x else b ternary operator (equivalent to the C/C++/Java expression x ? a : b) to put the conditional inside the list comprehension. This gives an efficient, Pythonic implementation:

    def fY(a, b):
        return [[aij/float(aij + bij) if aij+bij != 0 else 0 for aij, bij in zip(ai, bi)]
                for ai, bi in zip(a, b)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Lists, they look like this <List> ads [0] Headline = Sony
I have two lists. They are sortable but not connected (elements of list one
We have two lists: a=['1','2','3','4'] b=['2','3','4','5'] How to get a list with elements that
I have two lists which are guaranteed to be the same length. I want
I have two lists: listA: [[Name: mr good, note: good,rating:9], [Name: mr bad, note:
I have two elements: 1. A parent of fixed height, overflow:hidden 2. Its child,
This I'm assuming is a relatively trivial problem. I have two connected sortable lists,
I have two Lists of dictionaries. Both are not not null. How to merge
I have two lists. Ex: List<string> expected = new List<string>(); expected.Add( a ); expected.Add(
I have two lists: [a, b, c] [d, e, f] I want: [a, d,

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.