Python list, tuple operations

Source: Internet
Author: User

The Python list is used to hold a set of data represented by [].

1, List of common methods:

names = ['!alex','Eric','Rain','2kity']names.append ('Knochkapoor')#Add a single elementNames.count ('Alex')#statistics Alex NumberNames.insert (0,'Jlliu')#insert ' Julliu ' in the first positionNames.remove ('Jlliu')#delete ' Jlliu 'Names.pop ()#Delete the last one without parameters by defaultdel[Names[0]]#Delete the first element of a namesNames.index ()#Get subscriptNames.extend ()#List ExtensionNames.reverse ()#list top nephew, reversalNames.sort ()#List sort default First sign, then number, last letterNames.clear ()#clears the elements in the listNames[0:3]#take the first three values of the list, the slice of the list is Guxio regardless of the tailNAMES[-2:-1]#take the second value of the list, because the list's ' ignore the tail ' so the last one will not be taken outNames[-2:]#fetch the last two values of a listnames[:]#full slices of the listPrint(Names[::2])#An element that takes a list acrossEnumerate (names)#print out the subscript of the list forV,iinchEnumerate (names):#print out the subscript of the list    Print(v,i)

The use of list copy (), copy only one layer

name = ['ABC','BBB','CCC',['LJL','YHC',['Olace', 66,'MNBV'],'Lyw'],'FFF']c_name=name.copy ()Print(name)Print(c_name) name[3][2][0] ='SQL'name[3][1] ='QAZ'name[2] ='Zaq'Print(name)Print(C_name)#The above output # Note the list of elements in the list is changed, but the outermost list of the element quality is not changed['ABC','BBB','CCC', ['LJL','YHC', ['Olace', 66,'MNBV'],'Lyw'],'FFF']['ABC','BBB','CCC', ['LJL','YHC', ['Olace', 66,'MNBV'],'Lyw'],'FFF']['ABC','BBB','Zaq', ['LJL','QAZ', ['SQL', 66,'MNBV'],'Lyw'],'FFF']['ABC','BBB','CCC', ['LJL','QAZ', ['SQL', 66,'MNBV'],'Lyw'],'FFF']

Full copy () of the list

ImportCopy#introduction of a copy modulename = ['ABC','BBB','CCC',['LJL','YHC',['Olace', 66,'MNBV'],'Lyw'],'FFF']c_name=copy.deepcopy (name)Print(name)Print(c_name) name[3][2][0] ='SQL'name[3][1] ='QAZ'name[2] ='Zaq'Print(name)Print(c_name) Print results#at this time, C_name has a copy of the whole, not affected by the name['ABC','BBB','CCC', ['LJL','YHC', ['Olace', 66,'MNBV'],'Lyw'],'FFF']['ABC','BBB','CCC', ['LJL','YHC', ['Olace', 66,'MNBV'],'Lyw'],'FFF']['ABC','BBB','Zaq', ['LJL','QAZ', ['SQL', 66,'MNBV'],'Lyw'],'FFF']['ABC','BBB','CCC', ['LJL','YHC', ['Olace', 66,'MNBV'],'Lyw'],'FFF']

2, meta-group

Tuples are actually similar to the list, but also to save a group of numbers, it is not once created, it can not be modified, so it is called a read-only list, the expression of tuples with () to the table

There are only 2 methods, one is count, the other is index

Names = ('Alex','Eric','Rain','kity','Alex','Knoch')Print(Names.count ('Alex'))Print(Names.index ('Alex'))#If there are multiple identical elements, only the subscript of the first element is printed

Python list, tuple 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.