Zero basic learning python _ list and Meta Group (course 10-13), python10-13

Source: Internet
Author: User

Zero basic learning python _ list and Meta Group (course 10-13), python10-13

Today, I will go back and look at the list and metadata. Let's talk about the list. The list is actually the most frequently used data type in python. It is not only often used but also powerful, this is similar to the array in the C language, and the list can also be added, deleted, modified, and queried. However, I am not planning to explain it to you through the dictionary, I just want to explain the common methods in the list.

1. First, let's talk about how to create a list. You can see that list1 creates an empty list and list2 creates a Value List.

 

2. list1.append (). This method is appended to the end of the list. See the following example:

Remember that the append () method is append at the end of the list.

Although this type of end append can be added, What can I do if I want to insert it in the middle? The following method satisfies this requirement.

3. list1.insert (index, element). insex is the index number, which is the nth digit. The position starts from 0. Basically, the first index of the language is 0, element is the value you want to add to the list. All values are supported. You can add the list as follows:

Of course, you said that I only know that the list has a value and I want to add the new value to the value of the list. What should I do? Are you sure you want to insert the index based on the index first?

4. list1.index (value) indicates the value in the dictionary. The index number of the current value is returned (only the first value found is returned ), if you enter a list that does not exist, the program will report an error. The example is as follows:

Have you ever found that if you use insert to add a list, it will still be a list? I want to add the value of a list to another list, but it will not be displayed as a list after it is added? The list of insert statements is as follows:

5. list1.extend (list2): add multiple values in the list at the end of the list. Modify the extended sequence, and the connection operation returns a new list.

List1 is the sum of two lists. Remember that the new list will not be de-duplicated.

The following describes how to delete a list.

6. list1.pop (index) removes an element from the list. The default value is the last one. It is the only list operation that can modify the list and return element values.

Remember that pop is followed by the index number. If the index number does not exist, an error will be reported. If the index number is left blank, the last one will be deleted.

It is good to remove list elements through indexes, but sometimes you only know that the elements to be removed do not know the position of the elements. If you use indexes, the list is too large.

7. list1.remove (element): removes the first matching item of a value in the list.

Check that the first hello is removed, but what if we want to remove the second or all hello messages?

Here, let's talk about how to remove all the elements. Currently, the method that I think of is to use the remove method. We can first calculate the number of elements to be deleted in the list, then, I will use a for loop to execute the statistics for so many times. Will it be deleted? I don't talk much about posting code for you to see (the count method is as follows: 8th ):

Are all hello Messages deleted? Of course there may be better ones. I think of it now. Thank you for your advice. What if I want to delete the second or third one? After thinking about it, I wrote a rough version, and later I learned a little more and then revised it into a better way.

Def delete_element (list1, element, num): delete_num = 0 if element in list1: if num = 'alldelete': for I in range (list1.count (element )): list1.remove (element) else: num = int (num) while num: print (num) delete_num = list1.index (element, delete_num) delete_num + = 1 list1.pop (delete_num-1) else: print ("the value you entered is not in the list! ") List1 = [1, 2, 'Hello', 3, 4, 'Hello', 'word ', 'Hello'] element = input ("Enter the element you want to delete:") num = input ("Enter the elements you want to delete: (delete all alldelete )") delete_element (list1, element, num) print (list1)

8. list1.count (element): counts the number of times an element appears in the list.

Of course, you can also clear all the elements in the list one by one. Just use clear. This is similar to the dictionary method.

9. list1.clear (). This is not demonstrated, because the function is obvious. It is to clear the value of the list, but the list is still there. This is different from the del of the dictionary, it's just the content of the situation list, and the object is not cleared.

10. list1.copy (). Remember to copy a copy. The original value and the new copied variable do not affect each other. Different from =, = is an object reference.

The above describes so many list methods. Next we will talk about the sorting of the list. This may be a little difficult to use. Maybe it is really!

11. list1.sort (key = None, reverse = False). This is the forward sorting of the list. This method does not return values. Below is a simple sorting. You can see that this is the simplest usage, the default value is None.

The next step is more complex usage. Next we will sort the second bit of each element in the list.

Now that we have talked about the list of sort, we have to mention the function sorted supported by python itself. In fact, sorted is usually used more often, because it is more flexible. The following describes the usage:

Sorted (iterable [, key] [, reverse])The first parameter of sorted is an iterator, the second parameter is the key used for sorting, and the third parameter is in the ascending or descending order. This method returns the list.

Remember that the sort in the list changes the original list, while the sorted method does not change the original list.

The list is used for positive sorting, and the key, that is, the sorting rule, is the abs absolute value. Therefore, the sorting is shown in the figure.

By default, reverse is Flase. If it is set to True, it turns to reverse sorting. the following result is the opposite of the previous one.

12. list1.reverse (), an element in the reverse list. This method has no return value. You can see the example below.

Returns the list.

 

Well, the list method is described above. Next let's talk about the metagroup. The Python tuples are similar to the list. The difference is that the elements of the tuples cannot be modified. The tuples use parentheses, And the list uses square brackets.

Creating tuples is simple. You only need to add elements in brackets and separate them with commas.

1. For the creation of tuples, see the following example. You can create a tuples directly by tup1 = (). If the tuples have only one value and you want to create them as tuples, add a comma after the elements,

There are few methods for tuples, because they cannot be modified, only count and index

2. tup1.count (element) and tup1.index (element). I will not talk about these two items as much as the list. Let's take a look at them for an example.

3. List and tuples are converted to each other. In fact, this is nothing to mention, but it will be used later, so let's talk about it for everyone! Continue viewing instances:

Check whether it is easy. You just need to use list and tuple.

4. Finally, Let's explain the slice. This is awesome. Can you see if the following instances can be understood? Remember that the index starts from 0, and the slice sequence number is as long as the left boundary does not have the right boundary.

 

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.