My current code is ugly, so I'm looking for a more generic solution:
I have the following example python dictionary, with values as strings:
example_dict = {'key1':'AAAAAAAAAAA','key2':'BBBBAAABBBB', 'key3':'CAAAAAAACCC','key4':'DDDAADADAAA'}
Note that the strings are the same length, and only contain capital letters:
'AAAAAAAAAAA'
'BBBBAAABBBB'
'CAAAAAAACCC'
'DDDAADADAAA'
I would like to iterate over these strings, and find the indices whereby the string characters are the same. The following approach is what I'm not doing:
for i in range(11):
if (example_dict['key1'][i]==example_dict['key2'][i]==example_dict['key3'][i]==example_dict['key4'][i]):
print(i)
which works, as we see index 4 and 6 for all strings contain "A"
.
The problem is, I would like to do the same string comparison with other dictionaries, which have different key names (and more/less items).
For example, if I use the dictionary ex2 = {'one':'AAAAA', 'two':'ABBBA'}
, I would need to change the above if statement to
if (ex2['one'][i]==ex2['two'][i]):
Is it possible to do comparisons of python dictionary values without explicitly naming the keys? Naturally, the above could is very ugly----the better question could be, is there a more generic way to do this task?