mardi 19 février 2019

Python 3: Dict comprehension with if else and appending to a list

I have a list of dictionaries of the following structure:

myList=[{"ID": someString, "sequence": someString2, "quality":[list of numbers]}]

The quality has a list of numbers as value. The first number is representing the quality at position 0, the second at position 1 etc.

I want to create a quality-per-position Dictionary of the following structure:

qualityDict={0:[list of all first quality numbers], 1:[list of all second quality numbers], etc}

my Idea:

qualityDict={}
for entryDict in myList:
        qualityDict={pos:[(entryDict['quality'][pos])] if pos not in qualityDict.keys() else qualityDict[pos].append(entryDict['quality'][pos]) for pos in range(0,len(entryDict['quality']))}

The qualityDict is build as pos:[value] dictionary for each pos in range(0,length of the current quality). If the key is not already present in the dict, it is set and the value is set, if it is present, I want to append to the value.

So, after the first iteration for example, I have a dict with 0,1,2,3....length of first quality (which was iterated) as keys and each key has a list with one value. In the second iteration, I want to append the then current quality to the existing value's list.

I am getting an error for the appending part: 'NoneType' Object has no attribute append

What am I missing? Thanks :)

Aucun commentaire:

Enregistrer un commentaire