I don’t know why, but the dict at the end of this function won’t print fully. It will only print up to four keys+values and they’re only the first four. Curiously, the 3rd and 4th come out in opposite spots.
genelist = ['ABC', 'abc', 'Abc', 'aBC', 'ABc', 'abC', 'AbC', 'aBc']
def recombAB(x):
rec_total = 0
primlistAB = []
for item in x:
split = list(item)
del split[2]
primlistAB = primlistAB + split
listAB = [primlistAB[0] + primlistAB[1], primlistAB[2] + primlistAB[3], primlistAB[4] + primlistAB[5], primlistAB[6] + primlistAB[7], primlistAB[8] + primlistAB[9], primlistAB[10] + primlistAB[11], primlistAB[12] + primlistAB[13], primlistAB[14] + primlistAB[15]]
print(listAB)
dictAB = {listAB[0] : freq1, listAB[1] : freq2, listAB[2] : freq3, listAB[3] : freq4, listAB[4] : freq5, listAB[5] : freq6, listAB[6] : freq7, listAB[7] : freq8}
print(dictAB)
recombAB(genelist)
This gives me listAB = [‘AB’, ‘ab’, ‘Ab’, ‘aB’, ‘AB’, ‘ab’, ‘Ab’, ‘aB’]
And dictAB = {‘AB’: 9, ‘ab’: 9, ‘aB’: 1, ‘Ab’: 1}
When what I’m looking for is {‘AB’:479, ‘ab’:473, ‘Ab’:15, ‘aB’:13, ‘AB’:9, ‘ab’:9, ‘Ab’:1, ‘aB’: 1 }
Any help would be much appreciated, thanks.
When I run your code partly, by calling the function with
genelistas its parameter, I get the following output forlistAB:If you look closely, you have duplicate values in there, and essentially just 4 different values.
So when you build your dictionary, you assign multiple values to the same keys, overwriting the previous value.
A dictionary is a 1-to-1 map from a key to a value. This means that every key uniquely identifies a single element in the dictionary. But in your desired dictionary output, you would have single keys mapping to multiple different values (e.g.
ABto479and9). This is not possible.If you don’t need the map-property but just want to store pairs of values, you could use a list of tuples instead:
Or if you actually need to be able to lookup values from their “key”, you could make a multi-value dictionary by just mapping to a list of values: