Python Notes--list

Source: Internet
Author: User

Lists List

One of the data types built into Python isthe list: lists. A list is an ordered set of elements that can be added and removed at any time.

First, Definition List

>>> list1=[' A ', ' B ', ' C ',1,2,3]>>> list2=[' A ', ' B ', ' C ', [[+]]

From the above two lists, you can summarize the features of the list as follows:

1. Enclose the elements in brackets, separating them with commas.

2. Strings need to use single quotes, integers are not used.

The data type of the elements inside the 3.list can also be different.

The 4.list element can also be another list.

Second, Basic operation of the list

#查看列表元素个数

>>> Len (list1) 6

#用索引来访问列表中每一个位置的元素, the index is starting from 0

>>> list1[0] ' a ' >>> list1[1] ' B ' >>> list1[-1]3>>> list2[-1][0]1

Note: The index of the last element is-1, and so on, the penultimate one is -2... The index is not out of range or a indexerror error is reported.

#追加一个元素

>>> List1.append (4) >>> list1[' A ', ' B ', ' C ', 1, 2, 3, 4]

#删除指定内容的元素

>>> list1.remove (' a ') >>> list1[' B ', ' C ', 1, 2, 3, 4]

#根据索引删除元素, if you do not specify an index, the default is to remove it from the last start.

>>> list1.pop (0) ' B ' >>> list1[' C ', 1, 2, 3, 4]

#统计列表中相同元素的个数

>>> List1.count (1) 1

#统计指定元素的索引号

>>> list1.index (' C ') 0

#在指定索引的元素之后添加一个元素

>>> List1.insert (1, ' d ') >>> list1[' C ', ' d ', 1, 2, 3, 4]

#倒序列表排序

>>> List1.insert (1, ' d ') >>> list1[' C ', ' d ', 1, 2, 3, 4]

#正向排序元素

>>> list1.sort () >>> list1[1, 2, 3, 4, ' C ', ' d ']

#把一个列表扩展到另一个列表中

>>> list1.extend (list2) >>> list1[1, 2, 3, 4, ' C ', ' d ', ' A ', ' B ', ' C ', [1, 2, 3]

#列表连接

>>> list1+list2[1, 2, 3, 4, ' C ', ' d ', ' A ', ' B ', ' C ', [1, 2, 3], ' A ', ' B ', ' C ', [1, 2, 3]

Note: The elements in the list have not changed

#清空列表

>>> list1=[]>>> Len (list1) 0

Summarize:

The index number of the list starts from 0;

You can use DIR () to see how the list is used, and enter the name of the list in parentheses;

using Help () to view the actions, enter the name of the list in parentheses. Actions , such as:

>>> Help (List1.insert)-On built-in function Insert:insert (...) L.insert (Index, object)--Insert object before index


Add:

sometimes we need to define the results of a list reordering to a new list, without changing the original list, using sorted () and reversed ()

sorted () is ( list, dictionary ) < span> sorted () The function requires a parameter ( Parameters can be lists, dictionaries, tuples, strings

>>> a=[4,3,2,1]>>> b=sorted (a) #实现正向排序后定义给另一个列表 >>> b[1, 2, 3, 4]>>> a# List A has not changed [4 , 3, 2, 1] >>> c=sorted (b,reverse=true) #实现倒序排列后定义给另一个列表 >>> c[4, 3, 2, 1]>>> a[4, 3, 2, 1]

Reversed () is also A built-in function of Python that returns a reverse-access iterator to the sequence, which can be a list, a tuple, a string, and not change the original object, for example:

>>> a=[1,2,3,4]>>> b=reversed (a) >>> B<listreverseiterator object at 0x0000000000602b00 > #b是一个迭代器 >>> c=list (Reversed (a)) #实现反转一个列表的结果定义给另一个列表 >>> c[4, 3, 2, 1]>>> a[1, 2, 3, 4]

Third, slices

You can use Shard operations to access elements within a certain range. Shards are implemented by two indexes separated by a colon:

shards are useful for extracting part of a sequence. The principle of slicing is " take head without ending ", that is, the first index is the number of the first element to extract, and the last index is the number of the first element of the remainder after the Shard.

For example:

>>> list2[' A ', ' B ', ' C ', [1, 2, 3]]>>> list2[1:3][' B ', ' C ']

If you do not specify the first index, the default is to start from scratch; If you do not specify a second index, the default is cut to the end, for example:

>>> list2[:3][' A ', ' B ', ' C ']>>> list2[2:][' C ', [1, 2, 3]

#步长切片

At the time of the Shard, the beginning and end of the Shard need to be specified, the elements of the sequence are traversed by step, and all elements between the start and end points are returned. For example:

>>> list3=[0,1,2,3,4,5,6,7,8,9]>>> list3[::2][0, 2, 4, 6, 8]>>> list3[1::2][1, 3, 5, 7, 9]

Practice

1. Create an empty list

>>> lst1=[]

2. Add ten list elements, including strings, numbers

>>> lst1=[0,1,2,3,4, ' A ', ' B ', ' C ', ' d ', ' e ']

3. Inserting 5 elements from the first 5 Index numbers

>>> Lst1.insert (5,5)

>>> Lst1.insert (5,6)

>>> Lst1.insert (5,7)

>>> Lst1.insert (5,8)

>>> Lst1.insert (5,9)

4. Remove the first 5 , and the last 5 , respectively, using the slicing method

>>> Lst1[:5]

[0, 1, 2, 3, 4]

>>> lst1[-5:]

[' A ', ' B ', ' C ', ' d ', ' e ']

5. remove odd items, and even items separately

>>> Lst1.sort ()

>>> Lst1[::2]

[0, 2, 4, 6, 8, ' A ', ' C ', ' e ']

>>> Lst1[1::2]

[1, 3, 5, 7, 9, ' B ', ' d ']

6.print out list element number

>>> Len (lst1)

15

7. Delete an element

>>> lst1.remove (' a ')

8. Delete the last element of the list

>>> Lst1.pop (-1)

E

9. Delete the list of the first 5 elements

>>> Lst1.pop (4)

4


This article is from the "Network Technology" blog, please be sure to keep this source http://fengjicheng.blog.51cto.com/11891287/1927616

Python Notes--list

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.