Python (ii) List and operations

Source: Internet
Author: User

1. Creation of the list

Alist = [123, ' abc ', 4.56,[' inner ', ' list '], ' dchen ', ' Blank sapce '] #方刮号为列表, the array inside of it is added as its own case

2. Slicing operations on a list

Print (alist[0]) #选择列表第一个元素, the first element of the list in Python starts with 0
[' 123 ']

Print (Alist.index (123)) #找到元素在列表中的位置
0

Print (Alist[1:4]) #切片为数组中地址为1到4的元素 (4th digit not included)
[' Blank sapce ', ' dchen ', [' inner ', ' list '], 4.56, ' abc ', 123]

Print (Alist[:3]) #切片数组中地址为0到3的元素 (does not contain 3rd bit), the first bit does not give the specified number of points when the default is 0
[123, ' abc ', 4.56]

ALIST[3][1] #选择列表中子列表的元素
[' List ']

Alist[1:] #切片数组中地址为1到最后一位元素, the latter do not give the specified number of points when the default is the last
[' abc ', 4.56, [' Inner ', ' list '], ' dchen ', ' Blank sapce ']

ALIST[2:-1] #切片为数组中地址为2到最后一位的元素 (does not contain the last one)
[4.56, [' Inner ', ' list '], ' dchen ']

3. Add, delete, change the list
Append

Alist.append (345) #添加一个元素到列表中, added as last seat
Print (alist)
[123, ' abc ', 4.56, [' Inner ', ' list '], ' dchen ', ' Blank sapce ', 234]

Del

Del Alist[0] #选择删除列表中第一位元素
[' abc ', 4.56, [' Inner ', ' list '], ' dchen ', ' Blank sapce ']
Del alist #删除整个数组, this array will be emptied in memory

Pop

Alist.pop () #该删除方法在不给出任务参数时默认
[123, ' abc ', 4.56, [' Inner ', ' list '], ' dchen ']

Clear ()

Alist.clear () #和del The alist effect, empty the entire array and empty it inside

Remove

Alist.remove (4.56) #选择列表中的元素进行删除

Python (ii) List and operations

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.