The Python Dictionary {} Saves the data as a key-value pair and can access the saved values in the dictionary with the keys instead of the subscript. Dictionaries can contain almost any number of variables, dictionaries, sequences, and tuples. The sequence is the same.
The list of Python [] differs from a dictionary in that the list saves the content through a single element, with the subscript accessing the element.
Python dictionary nested list implementation is {key1:[], key2:[],...}
where the Append () method is used to add a new object at the end of the list. The Python dictionary SetDefault () function is similar to get (), and if the key does not exist in the dictionary, the key is added and the value is set to the default value. The Python dictionary in operator is used to determine if the key exists in the dictionary, or False if the key returns true in the dictionary dict. (Python2 or Has.key method)
A result statistic that is recently implemented uses this approach to achieve statistics on different IDs.
The code is as follows:
#Build a dictionary forId,fileinchEnumerate (IMG):ifFile.find ('GT') ==-1: Predict=file Label=img[id+1] Label_path=Os.path.join (root, label) Predict_path=Os.path.join (root, predict) ID= Predict.split ('_') [0]ifPatientidinchdict:dict[id].append (Label_path) dict[id].append (Predict_path)Else: Dict.setdefault (patientid,[]) dict[id].append (Label_path) dict[id].append (predict_p ATH)
You can then iterate through the key in the dict and then iterate through the list by Dict[key].
Python: Dictionary nested list