I have some code that's a little sloppy/repetitive where I want to generate a new dictionary after determining whether values from a list of lists match a "master list", returning either true or false. I want these new true/false lists appended to a dictionary using labels from yet another list. As such, each of the new lists (a-d below) will have the same number of values (either True/False for each) which will then be used as a dataframe index in generating an upset plot. The example code is as follows:
a = []
b = []
c = []
d = []
for motif in unique_motifs:
if motif in motif_lists[0]:
a.append('True')
else:
a.append('False')
for motif in unique_motifs:
if motif in motif_lists[1]:
b.append('True')
else:
b.append('False')
for motif in unique_motifs:
if motif in motif_lists[2]:
c.append('True')
else:
c.append('False')
for motif in unique_motifs:
if motif in motif_lists[3]:
d.append('True')
else:
d.append('False')
data_dictionary = {'motif_key': unique_motifs, \
args.plot_labels[0]: a, \
args.plot_labels[1]: b, \
args.plot_labels[2]: c, \
args.plot_labels[3]: d}
and here are some example values for each of the lists in motif_lists
and the master list unique_motifs
:
unique_motifs = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
motif_lists[0] = ['B', 'C']
motif_lists[1] = ['A']
motif_lists[2] = ['B', 'C', 'E', 'G']
motif_lists[3] = ['D', 'F']
and the above code would then create new lists as follows:
a = ['False', 'True', 'True', 'False', 'False', 'False', 'False']
b = ['True', 'False', 'False', 'False', 'False', 'False', 'False']
c = ['False', 'True', 'True', 'False', 'True', 'False', 'True']
d = ['False', 'False', 'False', 'True', 'False', 'True', 'False']
which would then get appended to the dictionary. Each of the plot_labels
values is a string and will be used as a unique identifier. I'd really like to condense this code AND as a BONUS, I'd like this dictionary/number of list generated/for loops to be expandable according to len(motif_lists)
(which is determined by the number of file provided by the user input). There is already a check in place to make sure len(motif_lists) == len(args.plot_labels
. For example, if len(motif_lists) == 7
, I would end up with lists a, b, c, d, e, f, and g such as above. I imagine there's a way to do this with something like:
for n, val in enumerate(motif_lists):
globals()["list%d"%n] = []
and then I would just set a limit on user input values so it doesn't get out of hand...