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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:38:47+00:00 2026-06-03T03:38:47+00:00

So I have a python dict like: 1:[ red,blue,green] 2: [blue,blue,red].. and so on.

  • 0

So I have a python dict like:

 1:[ "red","blue","green"]
 2: ["blue","blue","red"]..

and so on.

and then i have another python dict:
score_dict = {

 pid: weight
 1: 2
 2: 20
 ...
}

So, what i want is..
in the first dict, count number of times two colors occur together.
and so on.
But that count be multiplied by their weight.

For example:

I want to find out how many times red an blue occured together in this list:

so for pid 1
red and blue occurs once.

 so this is (1*2) # 2 comes from the score_dict as pid1 has a weight 2

and then for second

there are two blue, red pairs I can form

so this is (1*20) + (1*20)

So total score for blue and red occuring together is 2 + 20 + 20 = 42

Also, how do i extend it to 3 colors?

Like if i have to find out “red” “blue” and ” green” occuring together?

  • 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-03T03:38:48+00:00Added an answer on June 3, 2026 at 3:38 am
    from collections import Counter
    
    dict1 = {1:[ "red","blue","green"], 2: ["blue","blue","red"]}
    
    weight = {1: 2, 2: 20}
    
    score = 0
    
    for k,v in dict1.iteritems():
        score += weight[k] * Counter(v)["red"] * Counter(v)["blue"]
    

    Result:

    >>> score
    42
    

    The last part of my code could be rewritten as generator comprehension:

    score = sum(weight[k] * Counter(v)["red"] * Counter(v)["blue"] for k,v in dict1.iteritems())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python dict that looks like: defaultdict(<type 'int'>, {u'RT': 1, u'be': 1,
I have Python nested list that I'm trying to organize and eventually count number
I have this dict in python; d={} d['b']='beta' d['g']='gamma' d['a']='alpha' when i print the
I have a dictionary in Python where the keys are pathnames. For example: dict[/A]
I have a project using python and i want to convert the php to
I have a python class hierarchy, that I want to extend at runtime. Furthermore
I have a Python datetime timestamp and a large dict (index) where keys are
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
I have question about Dictionaries in Python. here it is: I have a dict
I have a Python 2.x module.py file that looks like this: class A(object): KEYWORD

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.