Python _ introduction to four basic types of data structures

Source: Internet
Author: User
Tags string methods

Python _ introduction to four basic types of data structures

Data Structure: In layman's terms, it is a container that stores a large amount of data. Here we mainly introduce four basic data structures of Python:List, Dictionary, tuples, and set.

The format is as follows:

List: list = [val1, val2, val3, val4], in brackets;
Dictionary: dict = {key1: val1, key2: val2}, braces, and each element is a ING Group of keys with colons and val;
Tuples: tuple = (val1, val2, val3, val4), parentheses;
Set: set = {val1, val2, val3, val4}, braces.

1. List:

List = [val1, val2, val3, val4]

The most notable features of the list are:

Each element in the list is variable;
The elements in the list are ordered, that is, each element has a position;
The list can accommodate any object in Python.
Next, let's take a look at the addition, deletion, modification, and query of the list.

Add:

1 list = [1,'dwd',3.6]2 list.insert(0,'Python')3 print(list)

The insert method can be used to add elements to the list. The insert method must specify the location where the added element is added and the added content. The actual location of the added element is prior to the specified location element. If the specified location does not exist, it is added to the end of the list by default.

1 list = [1, 'dwd ', 3.6] 2 list [0: 0] = [9] 3 # [0: 0] indicates inserting a new element 4 list [3: 3] = ['a'] 5 #【 3: 3: Insert the new element 6 print (list) at the first position in the list)

The two methods mentioned above are to add a single element. In addition to adding a single element, you can also add multiple elements and implement them using the extend method.

1 list_a = [1, 'dwd', 3.6] 2 list_ B = ['python', 56, 'game'] 3 list_a.extend (list_ B) 4 # The extend method is used to append multiple values in the other list at the end of the list. 5 print (list_a)

 Delete:

 

1 list = [1,'dwd',3.6]2 list.remove('dwd')3 print(list)

In addition to the above remove method, you can also use the del keyword to delete the elements in the list:

1 list = [1, 'dwd', 3.6] 2 del list [0: 2] 3 # [0: 2] is to delete element 4 print (list) at the 1st and 2nd locations)

Change:

1 list = [1,'dwd',3.6]2 list[2] = 73 list[0] = 'start'4 print(list)

If you want to replace an element in the list, you can directly assign a value to the element at a position in the list. list [2] indicates the first element in the list.

Query:

The index of the List is similar to the index of the string mentioned in the previous article "Python entry _ string sharding and index and string method". It is also divided into two index methods: positive and negative, it can be indexed from the past to the back, or from the back to the back. For example:

List = [1, 'dwd', 3.6] print (list [1]) # print the 2nd position element print (list [-1]) # print the element print (list [: 2]) at the last position # print 1st and 2nd elements (list [1:]) # print 2nd to the last element

However, if you want to view the position of an element, this method is not suitable and the program reports an error. Because the list only accepts location-based index, you cannot use the element to find the location.

2. dictionary:

Dict = {key1: val1, key2: val2}

Many concepts in the programming world are derived from life, and so are dictionaries. This data structure is constructed using "name-content" like the dictionary we use. In Python, each element is a correspondence group of keys with colons and val, it is often called a key-value pair.

The features of the dictionary are as follows:

The elements in the dictionary must be in the form of key-value pairs;

The key cannot be repeated, but the value (val) can be repeated;

The key is unchangeable and cannot be modified. The value can be modified and can be any object.

Even if the dictionary contains duplicate keys, the duplicate keys only appear once. For example:

1 dict = {'A':'art','B':'big','C':'cute','C':'cute'}2 print(dict)

Next, let's take a look at the addition, deletion, modification, and query of the dictionary.

Add:

The dictionary does not have a ready-made insert method as in the list, but you can insert elements in this way. By default, the elements are inserted at the last position.

1 dict = {'A':'art','B':'big','C':'cute'}2 dict['D'] = 'dictionary'3 print(dict)

The extend method can be used to add multiple elements to the list. The update method is also available in the dictionary to add multiple elements.

1 dict = {'A':'art','B':'big','C':'cute'}2 dict.update({'D':'dictionary','E':'exam'})3 print(dict)

Delete:

Delete an element in the dictionary or use the del keyword.

1 dict = {'A':'art','B':'big','C':'cute'}2 del dict['B']3 print(dict)

It should be noted that, although the dictionary uses braces, brackets are still used for deletion.

Change:

If you want to modify the elements in the dictionary, you can directly assign a value to the key.

1 dict = {'A':'art','B':'big','C':'cute'}2 dict['B'] = 'beyond'3 print(dict)

Query:

When indexing a dictionary, like deleting a dictionary, brackets are used to store the dictionary key. That is to say, keys are used to index dictionary elements.

1 dict = {'A':'art','B':'big','C':'cute'}2 dict['B']3 print(dict['B'])

In the previous article "Python getting started _ Talking about string sharding and indexing and string methods", we mentioned string sharding. In the dictionary, strings cannot be sharded.

3. tuples

Tuple = (val1, val2, val3, val4)

The tuples in Python are similar to the list. The difference is that the tuples cannot be modified, similar to the solid version list, therefore, the addition, deletion, and modification methods that can be used in the list cannot be used in tuples, but the elements in the tuples can be indexed, similar to the list.

1 tup = (1,2,'s',7)2 print(tup[0])3 print(tup[-1])4 print(tup[1:])5 print(tup[:-2])

4. Set

Set = {val1, val2, val3, val4}

The concept of a set is somewhat similar to that of a mathematical set. Each element in a set is an unordered, non-repeating object. We can use a set to determine the subordination of data. Sometimes, we can use a set to remove repeated elements in the data structure.

The set cannot be sliced or indexed. In addition to the set operation, the set element can be added or deleted.

Set = {7, 1, 8, 3, 4, 5, 3} set. add (0) # add element 0set. discard (7) # Delete element 7 print (set) # The printed sets are arranged from small to small and deduplicated.

If the set is used later, the operation of the Set is expanded in detail.

I will introduce some tips on data structure later.

Operating Environment: Python version, 3.6; PyCharm version, 2016.2; Computer: Mac

The above Python entry _ Talking about the four basic types of data structure is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for the customer's house.

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.