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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:28:58+00:00 2026-05-29T05:28:58+00:00

I have a list with sublists in it. EG: ( [1, 2], [1, 56],

  • 0

I have a list with sublists in it. EG: ([1, 2], [1, 56], [2, 787], [2, 98], [3, 90]) which is created by appending values to it while running a for loop.

I am working in python, and i want to add the 2nd element of each sublist where the 1st elements are same. in my eg:
i want to add 2+56 (both have 1st index as 1), 787+98(both have 1st index as 2) and keep 90 as it is because there is just one element with 1st index as 3.

I’m not sure how to do this.

Here is my code:

import urllib, re
from itertools import groupby
import collections
import itertools, operator
text = urllib.urlopen("some html page").read() 
data = re.compile(r'.*?<BODY>(.*?)<HR>', re.DOTALL).match(text).group(1)// storing contents from the BODY tag
values = [line.split() for line in data.splitlines()] //List with the BODY data
/* values contain elements like [[65, 67], [112, 123, 12], [387, 198, 09]]
   it contains elements with length 2 and three. 
   i am just concerned with elements with length 3
   in the for loop, i am doing this, and passing it to 2 functions.*/

def function1 (docid, doclen, tf):
    new=[];
    avgdoclen = 288;
    tf = float(x[2]);
    doclen = float(x[1]);
    answer1 = tf / (tf + 0.5 + (1.5*doclen/avgdoclen));
    q = function2(docid, doclen, tf)
    production = answer1 * q //this is the production of 
    new.append(docid) // i want to add all the production values where docid are same.
    new.append(production)
    return answer1

def function2 (docid, doclen, tf):
    avgdoclen = 288;
    querylen = 12;
    tf= float(x[2]);
    answer2 = tf/(tf + 0.5 + (1.5*querylen/avgdoclen));
    return answer2

for x in values:
    if len(x)==3:
        okapi_doc(x[0], x[1], x[2])
        okapi_query(x[0], x[1], x[2])

I want to add all the production values where the docid are same. Now when i print new, i get the following output:

['112', 0.3559469323909391]
['150', 0.31715060007742935]
['158', 0.122025819265144]
['176', 0.3862207694241891]
['188', 0.5057900225015092]
['236', 0.12628982528263102]
['251', 0.12166336633663369]

this is not a list. when i print new[0][0] i get 1. I want to get 112 when i print new[0][0]. Is there something wrong with append?
[‘334’, 0.5851519557155408]

  • 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-05-29T05:28:59+00:00Added an answer on May 29, 2026 at 5:28 am
    import collections
    result = collections.defaultdict(int) # works like a dictionary
    # but all keys have a default value of zero
    for key, value in mylist:
        result[key] += value 
    
    print result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have List I want to sort Desc by Priority, which is int and
I have list in python which has following entries name-1 name-2 name-3 name-4 name-1
I have a list of sublists which is as List<List<nodes>> . I have stored
Suppose I have a list and want to divide it into sublists of n
i have list of rows that user select and i want to delete them,
I have list of files which contain particular patterns, but those files have been
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have a list of items. For each item there are sublists. It is
I have a List of MyObjects ... MyObject{ int id,String name}. Now I want
I haven't had any luck creating function that removes sublists. I have this list:

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.