-2

I have a list that looks something like:

a = [1, 2, 3, 4]
b = [2, 3, 5, 6]
c = [3, 4, 5, 6, 10, 12]
d = [2, 3, 6]
e = [2, 3, 4, 5, 6]

Using the code list(set.intersection(*map(set, [a, b, c, d, e]))) I am getting the following output:

[3]

Is there any way that I can get output where any number present in 2 or more list are given, something like:

[2, 3, 4, 5, 6]

Even better if I can get a dictionary, like:

{
2:[a, b, d, e], 
3:[a, b, c, d, e], 
4:[a, c, e], 
5:[b, c, e], 
6:[b, c, d, e]
}
abhi
  • 337
  • 1
  • 3
  • 12

4 Answers4

1

Here is what you wanted. Usage is: findSpanned(master_list,minimum_span)

The minimum_span parameter is the minimum number of lists to have the common element.

def findSpanned(m_l,m_s):
    all_elements = []
    all_elements_count = []
    final_list = []
    for temp1 in m_l:
        for temp2 in temp1:
            if temp2 not in all_elements:
                all_elements.append(temp2)
    for temp3 in all_elements:
        temp5 = 0
        for temp4 in m_l:
            if temp3 in temp4:
                temp5 = temp5 +1
        all_elements_count.append(temp5)
    for temp6 in range(len(all_elements)):
        if all_elements_count[temp6] >= m_s:
            final_list.append(all_elements[temp6])
    return final_list

a = [1, 2, 3, 4]
b = [2, 3, 5, 6]
c = [3, 4, 5, 6, 10, 12]
d = [2, 3, 6]
e = [2, 3, 4, 5, 6]
master_list = [a,b,c,d,e]
minimum_span = 2
list_you_want = findSpanned(master_list,minimum_span)
print(list_you_want)

Output:

[2, 3, 4, 5, 6]
Joshua
  • 551
  • 4
  • 13
1

This will work:

from collections import defaultdict

data = {
    'a': [1, 2, 3, 4],
    'b': [2, 3, 5, 6],
    'c': [3, 4, 5, 6, 10, 12],
    'd': [2, 3, 6],
    'e': [2, 3, 4, 5, 6],
}

my_dict = defaultdict(list)
for k,v in data.items():
    for x in v:
        my_dict[x].append(k)
# only keep keys (numbers) with 2 or more occurrences:
my_dict = {k: v  for k,v in my_dict.items() if len(v) >= 2}
Julien
  • 13,986
  • 5
  • 29
  • 53
1

If you see the question, it can be reduced to calculation of elements which only occur in one list and no other. Remove these and you are done. Its quite simple using numpy

# Sum of list, also has repeated elements
l = [a, b, c, d, e]
u_l = sum(l, [])

# using numpy get counts
nums, counts = np.unique(u_l, return_counts=True)

# Get numbers which occur only once and remove them
set(nums) - set(nums[counts==1])
eroot163pi
  • 1,791
  • 1
  • 11
  • 23
1

Retrieve the name of the variable is not easy and not particularly useful. However, I found this link: Getting the name of a variable as a string

You can use:

import inspect

def retrieve_name(var):
    callers_local_vars = inspect.currentframe().f_back.f_locals.items()
    return [var_name for var_name, var_val in callers_local_vars if var_val is var]

Then to find the Dictionary of all the numbers contained in each variable you can use this:

a = [1, 2, 3, 4]
b = [2, 3, 5, 6]
c = [3, 4, 5, 6, 10, 12]
d = [2, 3, 6]
e = [2, 3, 4, 5, 6]

# Put all the variable in a list
ALL_lists = [a, b, c, c, d, e]
# Store the numbers you have visited
visited_numbers = []
Dic_num_in_list = {}

for l in ALL_lists:
    for num in l:
        # Retrieve the variable name
        name_of_var = retrieve_name(l)[0]
        # If the number is not in the Dictionary, add it
        if num not in visited_numbers:
            Dic_num_in_list[num] = [name_of_var]
            visited_numbers.append(num)
        else:
            if name_of_var not in Dic_num_in_list[num]:
                Dic_num_in_list[num] = Dic_num_in_list[num] + [name_of_var]

print(Dic_num_in_list)

The result will be:

Dic_num_in_list = {
1: ['a'],
2: ['a', 'b', 'd', 'e'],
3: ['a', 'b', 'c', 'd', 'e'],
4: ['a', 'c', 'e'],
5: ['b', 'c', 'e'],
6: ['b', 'c', 'd', 'e'],
10: ['c'],
12: ['c']
}