For details about the list and tuple of python learning notes, pythontuple

Source: Internet
Author: User
Tags python list

For details about the list and tuple of python learning notes, pythontuple

Preface

I have recently looked at the basic knowledge of python again, and I feel that I am still unfamiliar with this knowledge. I still need to read through books when I need to use it, pay attention to the basics first-I want to read the python tutorial again, and learn some of the lessons I previously ignored to enhance my exercises and memories.

Now I have read four important data structures built in python: list, tuple, set, and dict. I just want to record the list and meta-group (tuple) areas that are easy to mix up and describe the list and meta-group (tuple) methods that are commonly used.

List and tuple)

First, we have a clear understanding of the concepts of list and meta-group (tuple). It is clear that list is a variable sequence, and tuple) is an unchangeable sequence. Both data structures are python built-in data types, which are very convenient to use.

How can we define these two commonly used data types. It is necessary to use the list and tuple methods when defining an object.

>>> a_list=[]>>> a_list[]>>> a_list2=set()>>> a_list2set([])>>> type(a_list)<type 'list'>>>> type(a_list2)<type 'set'>

The preceding two methods are used to define a blank list. This definition is clearer and easier to understand than the definition of C ++ ~! Defining an empty tuples is the same method, but defining a blank tuples is a waste of effort. But let's take a look at the show:

>>> a_tuple=()>>> a_tuple()>>> a_tuple2=tuple()>>> a_tuple2()>>> type(a_tuple)<type 'tuple'>>>> type(a_tuple2)<type 'tuple'>

Defining a blank tuples in this way makes no sense.

After defining the object, you can operate the object. Since tuple is a list that cannot be changed, its method is too small, so I will not talk about it. Here I will only discuss the list method.

We have defined a blank list. Since a list is created, it must be used to store data. Therefore, we first introduce adding elements to the list, there are three methods for adding elements to the list (as far as I know): append, extend, and insert.

The @ append method appends a new element to the end of the list. You can add them one by one. If you don't want to be so troublesome, please refer to the following method extend.

@ Extend: append multiple values to the other list at one time at the end of the list.

The @ insert method inserts an element anywhere in the list.

For detailed applications, see the following:

>>> a_list=[]>>> a_list.append(1)>>> a_list[1]>>> a_list.append('python')>>> a_list[1, 'python']>>> a_list.extend([3,'ruby','perl',8,'julia'])>>> a_list[1, 'python', 3, 'ruby', 'perl', 8, 'julia']>>> a_list.insert(1,'javascript')>>> a_list[1, 'javascript', 'python', 3, 'ruby', 'perl', 8, 'julia']>>> a_list.insert(2,2)>>> a_list.insert(4,'php')>>> a_list[1, 'javascript', 2, 'python', 'php', 3, 'ruby', 'perl', 8, 'julia']

Cool, isn't it strange? The elements in the list can be of different types.

After adding an element to the list, we should explain how to delete the element from the list.

The methods for deleting elements in the list are pop and remove.

@ Pop is a simple method. Remember to delete the elements returned by this method.

@ Remove: removes an element from the list.

Example:

>>> a_list[1, 'javascript', 2, 'python', 'php', 3, 'ruby', 'perl', 8, 'julia']>>> a_list.sort()>>> a_list[1, 2, 3, 8, 'javascript', 'julia', 'perl', 'php', 'python', 'ruby']>>> a_list.pop()'ruby'>>> a_list.pop(0)>>> a_list.remove(2)>>> a_list[3, 8, 'javascript', 'julia', 'perl', 'php', 'python']

Note the usage of pop. If the pop function has no parameters, delete the elements at the end of the list directly.

In addition, there are many list methods, such as sort (sorting), reverse (storing elements in the direction), count (calculating the number of identical elements in the list), index (Guiding Position )......

========================================================== ========================================================== ============================

Can immutable tuple be changed?

What if you want to change it somewhere when you use the tuples?

Of course, this problem can be solved. Python has two elements, list and tuple, to solve the problem.

It is to forcibly convert the tuples into a list, modify them, and then convert them into a metagroup. The workload is a little big and costly.

>>> a_tuple=(0,1,2,4,5,6,7,8,9)>>> list_=list(a_tuple)>>> list_.insert(3,3)>>> a_tuple=tuple(list_)>>> a_tuple(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Conversion between Python list and tuple

Convert list to tuple:

temp_list = [1,2,3,4,5]

Force convert temp_list:tuple(temp_list)

Check whether the conversion is successful:print type(temp_list)

Convert tuple to list:

temp_tuple = (1,2,3)

The method is similar. You can also perform forced conversion:list(temp_tuple)

Check whether the conversion is successful:print type(temp_tuple)

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

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.