Python Learning DAY2 data structure

Source: Internet
Author: User

Lists [List]:

Lists are the most basic data structures in Python, and each element in the list is assigned a location

Lists are also known as arrays, and arrays can be divided into one-dimensional arrays, multidimensional arrays

#one-dimensional arraysList = ['Denny','Jenny','liming','Lilei','Hanmeimei']#Multidimensional Arrays #two-dimensional arrayslist1=[1,2,3,4,[51,52,53,54],6,,78] #three-dimensional arraysList2=[1,2,3,4,[xiaoming[18,175],xiaola#columnlist = [' Chihuahua 1',' Chihuahua 2',' Chihuahua 3' ,' Chihuahua 4'] 
A1=LIST[0]A2= List[1]a3= List[2]a4= List[3]#press Subscript to mark the valuePrint('a No. 0 element:', A1)Print('a 1th element:', A2)Print('a 2nd element:', A3)Print('a 3rd element:', A4) Run result: D:\Python\Miniconda3\python.exeNo. 0 Element: Chihuahua 1 1th element: Chihuahua 2 2nd element: Chihuahua 3 3rd element: Chihuahua 4Process finished with exit code 0

 

List of basic operations: Add, delete, change, check

Increase:

1 #Increase2 Way One:3List = ['Denny','Jenny','Lilei','Hanmeimei']4 #Append add elements to the last position by default5List.append ('liming')6 Print(list)7 8 Operation Result:9D:\Python\Miniconda3\python.exeTen['Denny','Jenny','Lilei','Hanmeimei','liming'] One Process finished with exit code 0 A  -  - Way Two: theList = ['Denny','Jenny','Lilei','Hanmeimei'] - #fill in the subscript in the specified position -List.insert (2,'liming') - Print(list) +  - Operation Result: +D:\Python\Miniconda3\python.exe A['Denny','Jenny','liming','Lilei','Hanmeimei'] atProcess finished with exit code 0

By deleting

#DeleteWay One:#deletes the element at the specified positionList = ['Denny','Jenny','liming','Lilei','Hanmeimei']delList[3]Print(list) Run Result: D:\Python\Miniconda3\python.exe['Denny','Jenny','liming','Hanmeimei']process finished with exit code 0 mode two: List= ['Denny','Jenny','liming','Lilei','Hanmeimei']#Pop does not write to the specified location by default delete the last elementList.pop ()Print(list) Run Result: D:\Python\Miniconda3\python.exe['Denny','Jenny','liming','Lilei']process finished with exit code 0 way three: list= ['Denny','Jenny','liming','Lilei','Hanmeimei']#Pop can also delete an element at a specified locationList.pop (1)Print(list) Run Result: D:\Python\Miniconda3\python.exe['Denny','liming','Lilei','Hanmeimei']process finished with exit code 0 mode four: List= ['Denny','Jenny','liming','Lilei','Hanmeimei']#Remove Deletes the specified elementList.remove ('Jenny')Print(list) Run Result: D:\Python\Miniconda3\python.exe['Denny','liming','Lilei','Hanmeimei']process finished with exit code 0 mode five: List= ['Denny','Jenny','liming','Lilei','Hanmeimei']#Clear Listlist.clear ()Print(list) Run Result: D:\Python\Miniconda3\python.exe[]process finished with exit code 0

Change

#ChangeList = ['Denny','Jenny','liming','Lilei','Hanmeimei']list[1] ='xiaoming'Print(list) Run Result: D:\Python\Miniconda3\python.exe['Denny','xiaoming','liming','Lilei','Hanmeimei']process finished with exit code 0

Check

List = ['Denny','Jenny','liming','Lilei','Hanmeimei']#query according to subscriptA = List[1]#-1 Take the last elementb = list[-1]Print(a)Print(b) Result of operation: D:\Python\Miniconda3\python.exe Jennyhanmeimeiprocess finished with exit code 0

Python Learning DAY2 data structure

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.