I have multiple pairwise comparisons to do in order to find common items between the lists. My code below works. However, I would like to keep track of the names of the lists. which are geno1, geno2 and geno3. I'm not sure how to get the combinations refering to the variable names instead of the arrays. Although there are related questions on Stack overflow such as Getting method parameter names, I'm hoping to get a quite easy solution.Thank you in advance for your time.
import itertools #to get all pairwise combinations
geno1 = [1,2,3]
geno2 = [2,5]
geno3 = [1,2,4,5]
genotypes = [geno1,geno2,geno3]
combinations = list(itertools.combinations(genotypes,2))
for pair in combinations:
commonItem = [x for x in pair[0] if x in pair[1]]
print(f'{len(commonItem)} common items between {pair}')#Here instead of pair I want to know which pair of genotypes such as geno1 geno2, or geno1 geno3.
print(commonItem)
print()
CodePudding user response:
Create a dictionary, where the keys are the name of the list and values are the lists that you originally have. You could do something using locals()
if you didn't want to write out the name of the lists as strings, but it's pretty hacky and I wouldn't recommend it:
import itertools
geno1 = [1,2,3]
geno2 = [2,5]
geno3 = [1,2,4,5]
genotypes = {"geno1": geno1, "geno2": geno2, "geno3": geno3}
combinations = list(itertools.combinations(genotypes.items(),2))
for (fst_name, fst_lst), (snd_name, snd_lst) in combinations:
commonItem = [x for x in fst_lst if x in snd_lst]
print(f'{len(commonItem)} common items between {fst_name} and {snd_name}')
print(commonItem)
print()
Output:
1 common items between geno1 and geno2
[2]
2 common items between geno1 and geno3
[1, 2]
2 common items between geno2 and geno3
[2, 5]
CodePudding user response:
You're probably best off putting it all into a dictionary. Then you can get the combinations of the names first, and look at the actual lists only inside the loop:
import itertools
genotypes = {
'geno1': [1,2,3],
'geno2': [2,5],
'geno3': [1,2,4,5],
}
combinations = list(itertools.combinations(genotypes,2))
for left_name, right_name in combinations:
left_geno = genotypes[left_name]
right_geno = genotypes[right_name]
commonItem = [x for x in left_geno if x in right_geno]
print(f'{len(commonItem)} common items between {left_name} and {right_name}: {commonItem}\n')
CodePudding user response:
Where you want to treat the names as data, you should be storing them as data. A dict
will let you hold names and values together:
import itertools
genotypes = {
'geno1': [1,2,3],
'geno2': [2,5],
'geno3': [1,2,4,5],
}
combinations = itertools.combinations(genotypes.items(), 2)
for (k1, v1), (k2, v2) in combinations:
commonItem = [x for x in v1 if x in v2]
print(f'{len(commonItem)} common items between {k1} and {k2}')
print(commonItem)
Output:
1 common items between geno1 and geno2
[2]
2 common items between geno1 and geno3
[1, 2]
2 common items between geno2 and geno3
[2, 5]
For more context, see: