List of data structures

Source: Internet
Author: User

List
A list is a data structure that handles a set of ordered items, that is, you can store a sequence of items in a list. Imagine you have a shopping list that says what you want to buy, and you can easily understand the list. Just on your shopping list, it's possible that everything is on its own, and in Python, you're separating each item with a comma.
The items in the list should be enclosed in square brackets so that Python knows that you are specifying a list. Once you have created a list, you can add, delete, or search for items in the list. Since you can add or delete items, we say that the list is a mutable data type, that is, this type can be changed.
Quick start for objects and classes
Although I have been postponing the discussion of objects and classes, a little explanation of them now gives you a better understanding of the list. We will explore this topic in detail in the corresponding chapters.
A list is an example of using objects and classes. When you use the variable i and assign it a value, such as assigning an integer of 5, you can assume that you have created an object (instance) of Class (type) int. In fact, you can look at help (int) to get a better understanding of this.
Classes also have methods that define only the ground function for a class. You can use these features only when you have an object of that class. For example, Python provides the Append method for the list class, which lets you add an item at the end of the list. For example, the Mylist.append (' an item ') List MyList adds that string. Note that you use the point number to use the object's methods.
A class also has a field, which is a variable that is defined only for a class. You can use these variables/names only when you have an object of that class. Classes are also used by DOT numbers, such as Mylist.field.
Working with lists
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example 9.1 Use List

#!/usr/bin/python#Filename:using_list.py#This is my shopping listshoplist=['Apple','Mango','Carrot','Banana']Print 'I have', Len (shoplist),'Items to purchase'Print 'These items are:', forIteminchshoplist:PrintItem,Print '\ni also has to buy rice.'Shoplist.append ('Susan')Print 'My Shopping list is now', ShoplistPrint 'I'll sort my list row'Shoplist.sort ()Print 'Sorted Shopping list is', ShoplistPrint 'The first item I'll buy is', Shoplist[0]olditem=Shoplist[0]delShoplist[0]Print 'I bought the', OlditemPrint 'My Shopping list is now', shoplist


Output
[Email protected] python]#
[email protected] python]# python using_list.py
I have 4 items to purchase
These items are:apple Mango carrot Banana
I also has to buy rice.
My shopping list is now [' Apple ', ' mango ', ' carrot ', ' banana ', ' rice ']
I'll sort my list row
Sorted shopping list is [' Apple ', ' banana ', ' carrot ', ' mango ', ' rice ']
The first item I'll buy is Apple
I bought the apple
My shopping list is now [' banana ', ' carrot ', ' mango ', ' rice ']

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How it works
The variable shoplist is a list of someone's shopping. In Shoplist, we only store the name strings for what you buy, but remember that you can add any kind of object including numbers or even other lists to the list.
We also used the for. In loops recursively between items in the list. From now on, you must have realized that the list is also a sequence. The characteristics of the sequence are discussed in a later section.
Note that we used a comma at the end of the print statement to eliminate the line breaks that are automatically printed for each print statement. It's a bit ugly, but it's really simple and effective.
Next, we use the Append method to add an item to the list, as discussed earlier. We then verify that the item is actually added to the list by printing the contents of the list. The Print list simply passes the list to the print statement, and we can get a neat output.
Next, we use the Sort method of the list to sort the list. It should be understood that this method affects the list itself rather than returning a modified list-this differs from how the string works. This is what we call the list to be mutable and the string immutable.
Finally, but when we finished buying something in the market, we wanted to remove it from the list. We use the DEL statement to do the work. Here, we point out which of the items in the list we want to delete, and the Del statement removes it from the list for us. We indicate that we want to delete the first element in the list, so we use del Shoplist[0] (remember, Python counts from 0).

If you want to know all the methods defined by the list object, you can get complete knowledge through Help (list).

List of data structures

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.