0 Basic Learning Python_ lists and tuples (10-13 lessons)

Source: Internet
Author: User

On a whim today and back to make up the list and tuples, first of all, the list of Kazakhstan, the list is actually the most commonly used in Python data type, not only often used is also very powerful, and the C language inside the array is similar, the list of course can also be added and modified, but I do not intend to use the way the dictionary to explain , I would like to explain the following list of commonly used methods just fine.

1, first I first say how to create a list, you see below List1 is created an empty list, List2 is created a list of values.

 

2, List1.append (), this method is appended at the end of the list, see the following example:

Remember that the Append () method is appended to the end of the list

This end addition can be added, but sometimes I want to insert in the middle what to do? The next method is to meet this demand

3, List1.insert (index,element), insex as the index number, that is, the position is starting from 0, basically the first index of the language is the 0,element is the value of their own want to join the list, what values are OK, plus the list of rows, see the following example:

Of course then you say I only know that the list has a value, and I want to add the new value to the list after that value, then what? Are we going to have to hit that worthy index before you can insert it on the index?

4, List1.index (value), value is the values in the dictionary, returns the index number of the current value (only the first found), if you enter the list does not exist then the program will error, the example is as follows:

Have you found that using insert to join the list is still a list, then I want to add a list of values to another list but not in the form of a list to show how to do? Insert the join list as follows:

5, List1.extend (LIST2), add multiple values of the list at the end of the list, modify the extended sequence, and the join operation returns a completely new list.

List1 becomes a two list of the and, remember that the new list will not go heavy.

Can be added to delete the following list of methods to remove the

6, List1.pop (index), remove an element from the list; The default is the last one. Is the only list method that can modify the list and return the value of the element in the list operation.

Remember that the pop is followed by the index number, the index number does not exist, will be error, do not fill in the index number is to delete the last

It is good to remove the list element by index, but sometimes you only know that the element you want to remove does not know the location of the element, and if you use index it is cumbersome, so the list is more

7, List1.remove (Element) to remove the first occurrence of a value in the list.

See, remove the first Hello, but what if we want to remove the second or all hello?

Here first of all remove it, at present I think of the method is, still use the remove this method, we can first count the number of elements we want to delete in the list, and then use a for loop to execute just statistics so many times do not delete it? No more said put a code for everyone to see (count method see below the 8th):

Did hello all be deleted? Of course there may be better, at present this reminds me of, welcome everyone to teach ha, then if I want to delete the second or third one? Pondering the next oneself wrote a rough version, later learned to change to a better wording.

defdelete_element (list1,element,num): Delete_num=0ifElementinchList1:ifnum = ='Alldelete':             forIinchRange (List1.count (Element)): List1.remove (Element)Else: Num=int (num) whileNum:Print(num) delete_num=List1.index (element,delete_num) Delete_num+ = 1List1.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 ("Please enter the element you want to delete:") Num= Input ("Please enter the number of elements you want to delete: (Alldelete to delete all)") delete_element (list1,element,num)Print(List1)

8, List1.count (Element), count the number of occurrences of the element in the list

Of course, you can clear all the elements of the list at once except one clear, which is similar to the dictionary method.

9, List1.clear (), this does not demonstrate, because the role is obvious, is to empty the list of values, but the list is still in, this and the Dictionary del can be different oh, only the situation list content, not clear objects.

10, List1.copy (), this you remember is to copy a copy, the original value and the newly copied variables do not affect each other, and = different, = is the object reference.

It says so many of the methods of the list, the next is to say the following list of the sort, this use may be relatively troublesome point, perhaps really!

11, List1.sort (Key=none, Reverse=false), this is the list of the forward sort, the method does not return a value, the following is a simple sort, you see, this is the simplest use, the parameter default is None.

This is followed by a more complex usage, which is sorted by the second bit of each element in the list.

Now that we have a list of the sort, you have to mention the Python itself supports the function sorted, in fact, generally use sorted more, because 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 to sort, the order of the third parameter is ordered: positive or reverse, the method returns the list

Remember that the list sort is changing the original list, and the sorted method does not change the original list.

Just use the list to sort forward, and then key is the sort rule that is abs absolute, so the sort is shown in the figure.

The default reverse is flase, and if it is true it turns into a reverse sort, and the following results are the opposite.

12, List1.reverse (), reverse the list of elements, the method has no return value, see an example to know

is to reverse the list.

Well, here's a list of methods, and then the following is the tuple bar, Python's tuple is similar to the list, except that the elements of the tuple cannot be modified. Tuples use parentheses, and the list uses square brackets.

Tuple creation is simple, just add elements in parentheses and separate them with commas.

1, the creation of tuples, see the following example, create a tuple can be directly tup1 = (), if the tuple has only one value, but also want to create a tuple remember to add a comma after the element,

Tuple methods are less, because they cannot be modified, only count and index

2, Tup1.count (Element) and Tup1.index (element), these two and list almost I don't have to say, give an example everyone to see just fine

3, the list and the meta-Group of the mutual conversion, in fact, this also has nothing to say, but after the estimate will also be used, so it is said to everyone! Continue to see examples:

See, is not very simple, just use list and tuple can

4, finally to everyone to explain the next section, this is still relatively good, we look at the following examples can understand it? Remember the index is starting from 0, and the number of slices is as long as the left line not the right edge of the OH

5, suddenly think of something that seems to have not said, although the tuple can not be modified, but also through the "+" to the tuple connection, or through Del for the tuple delete operation.

0 Basic Learning Python_ lists and tuples (10-13 lessons)

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.