Sequence of first-time Python experiences-dictionary

Source: Internet
Author: User

The implementation of a language class library or data structure is particularly important for this language. Currently, unless it is very low-level, development efficiency is generally required, such as the Java JDK, he comes with a lot of useful classes, and the C ++ STL is also a very useful template library. Python is not an example. Here we introduce the implementation of two simple data structures, namely, sequences and dictionaries. In fact, it is very simple to hear this name. This sequence is actually a List interface in Java or a simple linked List. It may be an array implementation or pointer implementation, but the basic function is to Add data and Get data, then we can randomly access data based on the Index, which is basically a container for storing data or objects. It is called a sequence in Python. The dictionary is actually a key-value pair, which is equivalent to Map. More specifically, it is equivalent to LinkedHashMap in Java, this is because the dictionary does not disrupt the Hash Storage during traversal, but stores the original sequence in the form of a linked list. The following describes the sequence and dictionary examples. [Python] ''' Created on @ author: Administrator ''' shoplist = ['apple', 'Banana ', 'orange ', 'peach '] print ("I need to buy", len (shoplist), "seed fruit") print ("these fruits are") for I in shoplist: print (I) print ("I still have to buy") shoplist. append ("watermelon") print ("the current list is", shoplist) print ("I need to sort") shoplist. sort (key = None, reverse = False) print ("the sorted list is", shoplist) print ("the first product I want to buy is", shoplist [0]) del shoplist [0] print ("the current list after deletion is", shoplist) ''' Create D on @ author: Administrator ''' shoplist = ['apple', 'Banana ', 'orange', 'peach'] print ("I need to buy ", len (shoplist), "Fruit") print ("these fruits are") for I in shoplist: print (I) print ("I still have to buy") shoplist. append ("watermelon") print ("the current list is", shoplist) print ("I need to sort") shoplist. sort (key = None, reverse = False) print ("the sorted list is", shoplist) print ("the first product I want to buy is", shoplist [0]) del shoplist [0] print ("the current list after deletion is", shoplist) This example can easily explain the sequence. The basic CRUD is complete. Next let's talk about the dictionary [python] ''' Created on 2013-1-23 @ author: Administrator ''' mymap = {'A': 'hahaha', 'B ': 'bb', 'C': 'cc'} print (mymap ['a']) print ("length is", len (mymap) for name, address in mymap. items (): print (name, "=", address) mymap ['D'] = 'dd' for name, address in mymap. items (): print (name, "=", address) del mymap ['a'] for name, address in mymap. items (): print (name, "=", address) ''' Created on 2013-1-23 @ author: Administrator ''' mymap = {'A': 'haha ', 'B': 'bb', 'C': 'cc'} print (mymap ['a']) print ("length is", len (mymap) for name, address in mymap. items (): print (name, "=", address) mymap ['D'] = 'dd' for name, address in mymap. items (): print (name, "=", address) del mymap ['a'] for name, address in mymap. the items (): print (name, "=", address) dictionary is actually Map. CRUD is basically easy.

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.