Python3 list additions and deletions change the method of merging sorting
Source: Internet
Author: User
# Auther:aaron Fan
names = ["Aaron", "Alex", "James", "Meihengfan"]
Names2 = [1,2,3,4,5]
Print (names)
#查
#print (names) #列出列表的内容
Print (names[3]) #访问列表中第4个值
Print (Names[1:3]) #访问列表中从第2个到第3个的值
Print (names[-1]) #访问列表中的最后一个值
Print (Names[:-2]) #访问列表中的所有值, but remove all values from the penultimate and subsequent
Print (names[-3:]) #访问列表中倒数第一个到倒数第三个的值
Print (names[0],names[3]) #注意取多个值的时候, cannot directly write the subscript together, need to write in this way
Print (Names[::2]) #打印列表, but with 2 as the step, is to skip the cut, you can also change the step according to demand
Print (Names.index ("James")) #查找列表中james这个元素的下标
Print (len (names)) #确定列表的长度
#增
Names.append ("Jack") #在列表末尾插入一个元素
Names.insert (1, "Fanheng") #把fanheng插入到第二个位置那里
#改
NAMES[2] = "Liming" #把第三个位置的元素改成liming
#删
Names.remove ("Liming") #把元素liming从列表中删除
Del names[2] #把第三个元素删除, you must know the index of the element
#del names #直接删除列表
Names.pop () #默认删除列表末尾的元素, of course, you can also directly specify the index of the element to eject a specified element, and let you wait for it to continue to use it
#每当你使用pop时, the elements that are ejected are no longer in the list.
#pop把一个元素从列表中弹出来了, the value being bounced can be directly assigned to other variables, such as:
Popend_name = Names.pop ()
Print (Popend_name)
#names. Clear () #清空列表, hazardous operation, please use caution
#其它操作
#names. Reverse () #把列表反转, is to reverse the original order completely.
#排序
#names. Sort () #把列表永久性的排序
Print (sorted (names)) #对列表进行临时性的排序
#合并列表
Names.extend (Names2) #把names2的东西合并到names里面
Print (names)
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