Python Learning Notes (ii) List operations

Source: Internet
Author: User

List and list operations:

List is one of the most commonly used data types, lists are also called arrays, list definitions, use [] can be, a list of the list, a set of a list, called a two-dimensional array; A list inside a set, the list of a list, this is called three-bit array, a few layers is a few dimensions, the definition is as follows:

1List1 = [1,2,3,4]#an ordinary array2List2 = ['Marry','Lily', [50,' Money']]#two-dimensional arrays3LIST3 = ['name','Sex',['Lily', 124,['AAAA','BBB']]]#three-dimensional arrays
1 all_nums=[123,456,[789,10,11]]2 three=[123,456,[789,10,11,['hahah ','1234']]3print#Ten 4 Print # Hahah

Through the following access elements, subscript starting from 0 count, that is, such as a list, there are 5 elements, then it is the first element subscript is 0, the second is 1, and so on, the string also has subscript, and list , the operation of the list, the following kinds of increase, delete, change, check

Subscript, Angle mark, index:
  

1stus=['Xiaohei','Xiaobai','Xiaohuang','Xiaolan']2 Print(Stus[1])#' Xiaobai '3 Print(Stus[-1])#' Xiaolan ' The last element subscript can be written-14 Print(Stus[-2])#' Xiaohuang '

Increase:

msg='Hello'name=['Andashu','cc','Niuniu']name.append (msg)#add an element to the end of the listName.insert (1,MSG)#inserted from the specified position, this 1 represents the subscriptName.insert (10,MSG)#if the specified subscript does not exist, it is added at the endName.insert ( -1,MSG)#-1 can write, but the result is not added to the wrong, so generally do not writePrint(name)

Modify:

1 msg=' hello '2 name=['andashu',' cc ','niuniu']3 name[1]='Baby  '# Modify the value of the specified position and remove the label directly to modify it.

Inquire:

1 msg=' hello '2 name=['andashu',' cc ','niuniu']3print(name[0])#  Gets the first element 4print(name[-1])#-1 represents the last element
1stus=['Xiaohei','Xiaobai','Xiaohuang','Xiaolan']2 Print(Stus.count ('Xiaohei'))#View the number of an element in the list3 Print(Stus.count ('hahaha'))#View the number of an element in the list and return 0 if the element does not exist4 Print(Stus.index ('Xiaobai'))#Find the subscript for this element, if multiple, return the first one5 Print(Stus.index ('hahaha'))#find the subscript for this element; If multiple, return the first; If a non-existent element is found, an error will be found.

Delete:

1msg='Hello'2name=['Andashu','cc','Niuniu']3Name.remove ('cc')#deletes the specified value4Name.pop ()#The last element is deleted by default, and if the subscript is specified, the specified element is deleted5Name.pop (2)6Name.pop (100)#If you delete an element that does not exist, the error7 delNAME[0]#Delete the value at the specified location8Name.clear ()#Clear List

List operations, some built-in methods:

1a=[1,2,3,4]2A=x5,6,7,8]3 A.extend (b) #合并a, b two list4Print (a) #[1,2,3,4,5,6,7,8]5Print (b) #[5,6,7,8] Just merge the contents of column B into a, B list is not affected6 7Stus = ['Xiaohei','Xiaobai','Xiaohuang','Cxdser','Xiaohei']8 stus.reverse () #反转list9Print (Stus) #['Xiaohei','Cxdser','Xiaohuang','Xiaobai','Xiaohei']Ten  One  ANums = [9, to,345, A,457,2352,12143,2321] - Nums.sort () -Print (nums) #[9, A, to,345,457,2321,2352,12143] theNums.sort (reverse=True) -Print (nums) #[12143,2352,2321,457,345, to, A,9] -#sort () sort, the default sort is ascending, and if Reverse=true is specified, it is sorted in descending order.

List loop:

If the direct for loop is a list, then the value of each loop is the element in the list.

1names=['haha','hehe','Heihei']2  forNameinchnames:3     Print(name)#haha hehe heihei4 5 6index=07  forIinchRange (3):8     Print(Names[index])9Index+=1#haha hehe heihe

Slice:

The slice is another way to get the value of the list, it can get more than one element, it can be understood, starting from the first few elements, to the end of the first few elements, get the value between them, the format is name:[1:10], for example, to get the first element of name to the fifth element, you can use Name[0:6 ], the slice is not included in the value of the next element, remember Gu Tou regardless of the tail, the front subscript if it is 0, you can omit not to write, so write, Name[:6], after the slice can write a parameter, called step, that is, how many elements, take once, default can not write, That is, the slice operation can also be used for strings, as is the case with lists, as follows:

1nums=[1,2,3,4,5,6,2,3,4,6,4,3,2,1,2,3,4]2 Print(Nums[3:6])#[4,5,6]3 Print(Nums[:6])#[1,2,3,4,5,6] If the previous value of the slice is not written, the number from the beginning4 Print(nums[3:])#[4,5,6,2,3,4,6,4,3,2,1,2,3,4] If the subsequent value of the slice is not written, take the end5 Print(nums[:])#if the two values of a slice are not written, take it from the beginning to the end6 Print(Nums[::2])#step is 2, this represents all the elements, and then every 2 elements take a7 Print(Nums[::-1])#step is positive words from left to right, step is negative to take from right to left, equivalent to reverse order8 Print(nums[-1:-9:-1])9 Print(nums[16:8:-1])Ten #slices also apply to strings, and strings have subscripts Onetitle='Today, Apple' #a space is also counted as a string A Print(Title[0])#Today - Print(Title[:4])#Today Hair -  forTinchTitle: the      Print(t)#Today, Apple -  forI,t Enumerate (title):#enumerate can cycle index subscript and value at the same time -     Print('%s:%s'% (i,t))#0: Today 1: Day 2: Fat 3:4: Apple 5: Fruit
#列表也适用enumerate:
names=[' haha ', ' hehe ', ' Heihei ']
For Index,name in Enumerate (names):
Print ('%s:%s '% (index,name))

Small exercise:

Registration: Enter the user name, determine whether the account exists, if there is a hint, if not, prompt registration success;

Python Learning Notes (ii) List operations

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.