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']
}