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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:35:01+00:00 2026-06-18T09:35:01+00:00

there is a dictionary containning many lists, for example, list_dic= { q1:[1,2,3,4,5] q2:[2,3,5] q3:[2,5]

  • 0

there is a dictionary containning many lists, for example,

  list_dic= {
    q1:[1,2,3,4,5]
    q2:[2,3,5]
    q3:[2,5]
    }

and I want to get all common items count for each list, e.g. the common items count for q1 and q2 is 3=(2,3,5)

q1={q2:3, q3:2}
q2={q1:3,q3:2}
q3={q1:2, q2:2}

my code for this task is:

result = {}
for name, source_list in list_dic.items():
    for target_name, target_list in list_dic.items():
        count = 0
        for item in source_list:
            if item in target_list:
                count+=1
    result[name][target_name] = count 

but this algorithm is inefficient, I want to know a better algorithm to do this task

  • 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-18T09:35:02+00:00Added an answer on June 18, 2026 at 9:35 am

    I think this should do it:

    import itertools
    import collections
    
    q1 = 'q1'
    q2 = 'q2'
    q3 = 'q3'
    
    dic_list = {
         q1:[1,2,3,4,5],
         q2:[2,3,5],
         q3:[2,5]
         }
    
    #sets are much more efficient for this sort of thing.  Create a dict
    #of the same structure as the old one, only with `set` as values 
    #instead of `list`
    dic_set = {k:set(v) for k,v in dic_list.items()}
    
    new_dic = collections.defaultdict(dict)
    for k1,k2 in itertools.combinations(dic_set,2):
         #to get the count, we just need to know the size of the intersection
         #of the 2 sets.
         value = len(dic_set[k1] & dic_set[k2]) 
         new_dic[k1][k2] = value
         new_dic[k2][k1] = value
    
    print (new_dic)
    

    If you’re following the comments, it turns out that combinations is slightly faster than permutations:

    import itertools
    import collections
    
    q1 = 'q1'
    q2 = 'q2'
    q3 = 'q3'
    
    
    dic_list = {
         q1:[1,2,3,4,5],
         q2:[2,3,5],
         q3:[2,5]
         }
    
    dic_set = {k:set(v) for k,v in dic_list.items()}
    
    def combo_solution():
         new_dic = collections.defaultdict(dict)
         for k1,k2 in itertools.combinations(dic_set,2):
              value = len(dic_set[k1] & dic_set[k2])
              new_dic[k1][k2] = value
              new_dic[k1][k2] = value
         return new_dic
    
    def perm_solution():
         new_dic = collections.defaultdict(dict)
         for k1, k2 in itertools.permutations(dic_set,2):
              new_dic[k1][k2] = len(dic_set[k1] & dic_set[k2])
         return new_dic
    
    import timeit
    print timeit.timeit('combo_solution()','from __main__ import combo_solution',number=100000)
    print timeit.timeit('perm_solution()','from __main__ import perm_solution',number=100000)
    

    with the results:

    0.58366894722    #combinations
    0.832300901413   #permutations
    

    This is because set.intersection is an O(min(N,M)) operation — Which is cheap, but can add up if you’re doing it twice as many times as you need to.

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

Sidebar

Related Questions

Is there a way to see how many items in a dictionary share the
I have the following code which produces a dictionary containing multiple lists; each list
is there a dictionary i can download for java? i want to have a
Is there a way to delete all the entries of a dictionary without deleting
Is there a way to obtain something like a dictionary of all key-value pairs
As the title says, is there a dictionary similar to Objective-C's NSDictionary for C++?
Hi in my code there is a dictionary of dictionary. nrec={'bridge': 'xapi1', 'current_operations': {},
Is there a simple description of how the dictionary file in Proguard should be
is there any way to store duplicate keys in a dictionary? I have a
There are several places on the internet that talk about having multy key dictionary

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.