List operations for the Python Getting Started tutorial

Source: Internet
Author: User
Tags python list
Python list operation--Increase

Append: Append a piece of data to the end of the list

name = ["Zhangsan", "Xiongda", "Lisi"]name.append ("Wangwu") Print name output: [' Zhangsan ', ' Xiongda ', ' Lisi ', ' Wangwu ']


Insert : Inserts a single piece of data at the specified location

name = ["Zhangsan", "Xiongda", "Lisi"]name.insert (1, "Wangwu")    #在下标为1的位置插入一条数据 "Wangwu" Print name output: [' Zhangsan ' , ' Wangwu ', ' Xiongda ', ' Lisi ']



Python list operation--delete

name = ["Zhangsan", "Xiongda", "Lisi"]name.remove ("Lisi")    #删除指定的数据print name output: [' Zhangsan ', ' Xiongda ']
name = ["Zhangsan", "Xiongda", "Lisi"]del name[0]    #删掉下标为0的一条数据print name output: [' Xiongda ', ' Lisi ']
name = ["Zhangsan", "Xiongda", "Lisi"]name.pop ()    #删除最后一条数据print name output: [' Zhangsan ', ' Xiongda ']



if a parameter is brought into the pop (), the effect is the same as the Del

Name.pop (1) = = del Name[1]



Python list operation--Change

name = ["Zhangsan", "Xiongda", "Lisi"]name[1] = "Wangwu" Print name output: [' Zhangsan ', ' Wangwu ', ' Lisi ']



Python list operations-check

name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu"]print (name[1])     #直接取出下标为1的数据输出结果: Xiongda
name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu"]print (Name[0:2])   #取出从下标0到下标1的数据, excluding 2 (Gu Tou regardless of tail) output: [' Zhangsan ', ' Xiongda ']



When the current is marked as a negative number, the right start takes

name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu"]print (name[-1])     #直接取出下标为1的数据输出结果: Wangwu
name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu"]print (name[-3:-1])   #从倒数第三个开始取, take the second-to-last, because it does not include the 1 output: [' Xiongda ', ' Lisi ']
name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu"]print (name[-3:])   #从倒数第三个开始取, take the first output of the countdown: [' Xiongda ', ' Lisi ', ' Wangwu '] Similarly from yesterday to take the time name[0:3] = = Name[:3]



List Other actions:

name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu", "Lisi"]name.count ("Lisi")  #统计Lisi出现的次数name. Clear ()         # Empties the data name.reverse ()       #反转列表name. Sort ()           forward sort name2 = ["1", "2"]name.extend (name2)  #两个数组合并, name2 on the back
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.