Python: Dictionary nested list

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.