Python sequence list and tuple common methods and considerations _python

Source: Internet
Author: User

Sequence sequence

A sequence (sequence) is a set of ordered objects. A sequence can contain one or more elements, or it can have no elements.

The basic data types that we have previously described can all be objects of a sequence. Object can also be another sequence. There are two kinds of sequences: list (table) and tuple (tuple).
The main difference between list and tuple is that once established, the elements of the tuple can no longer be changed, and the individual elements of the list may be changed.

List

To get the number of list elements:

Copy Code code as follows:

>>> lst=[' update slow ', ' python ', 5.44,false]
>>> Len (LST)
4

When referencing access, the index starts at 0 and is not crossed:

Copy Code code as follows:

>>> Lst[0]
' Update Slow '
>>> Lst[1]
' Python '
>>> Lst[2]
5.44
>>> Lst[3]
False
>>> Lst[4]
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
LST[4]
Indexerror:list index out of range

Index with-1 to get the last element directly:

Copy Code code as follows:

>>> Lst[-1]
False
>>> Lst[-2]
5.44
>>> Lst[-3]
' Python '
>>> Lst[-4]
' Update Slow '
>>> Lst[-5]
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
LST[-5]
Indexerror:list index out of range

Because the list is a mutable ordered table, you can append elements to the end of the list:

Copy Code code as follows:

>>> lst.append (' Add me one ')
>>> LST
[' Update slow ', ' python ', 5.44, False, ' Add me one ']

Append multiple elements at once:

Copy Code code as follows:

>>> lst.extend ([' A ', ' B ', ' C '])
>>> LST
[' Update slow ', ' python ', 5.44, False, ' Add me a ', ' a ', ' B ', ' C ']

To delete the element at the end of the list, use the Pop () method:

Copy Code code as follows:

>>> Lst.pop ()
C
>>> LST
[' Update slow ', ' python ', 5.44, False, ' Add me a ', ' a ', ' B ']

Deletes the element at the specified location, using the pop (i) method, where I is the index position:

Copy Code code as follows:

>>> Lst.pop (0)
' Update Slow '
>>> LST
[' Python ', 5.44, False, ' Add me one ', ' a ']

list element, which can be assigned directly to the corresponding index position:

Copy Code code as follows:

>>> lst[-1]= ' 100 '
>>> LST
[' Python ', 5.44, False, ' Add me one ', ' 100 ']

The list element can also be another list, and the inserted list is only one element:

Copy Code code as follows:

>>> Lst.append (Lst1)
>>> LST
[' Python ', 5.44, False, ' Add me a ', ' m ', [' 666 ', ' Qwer ']]
>>> Len (LST)
6

Tuple

Tuple can not be modified once initialized. A string is a special element, so you can perform related operations on tuples.

Copy Code code as follows:

>>> str= ' It's time to go to bed, goodnight! '
>>> print (Str[:7])
Time to go to bed, goodnight.

The immutable tuple meaning is that the code is more secure because it is immutable. If possible, use tuple as much as possible with tuple.

Copy Code code as follows:

>>> tuple= (' 1 ', ' 2 ', ' 3 ')
>>> tuple[0]=6
Traceback (most recent call last):
File "<pyshell#62>", line 1, in <module>
Tuple[0]=6
TypeError: ' Tuple ' object does not support item assignment

Define an empty tuple:

Copy Code code as follows:

>>> tuple1= ()
>>> Tuple1
()

Note that you want to define a tuple that has only 1 elements:
Copy Code code as follows:

>>> tuple2= (666,)
>>> Tuple2
(666,) #正确的
>>> tuple3= (666)
>>> Tuple3
666 #错误的, only 666 of this number is defined.

Note: Tuple's so-called "invariant" means that each element of tuple is never changed.

Copy Code code as follows:

>>> l=[' CCTV-5 ', ' HI ']
>>> tuple4= (' UFO ', ' HACK ', L)
>>> tuple4
(' UFO ', ' HACK ', [' CCTV-5 ', ' HI '])
>>> l[1]= ' I'll change first '
>>> tuple4
(' UFO ', ' HACK ', [' CCTV-5 ', ' I change first '])

So try to avoid this kind of apply.

Summary of the day

master list and tuple common methods and precautions.

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.