Python list, tuples

Source: Internet
Author: User

Python list:

The format of the list is:

list=["Index1", "Index2", "#list为列表名", index1,index2,1,2 as a list element.

The above is a simple list, how to get the elements in the output list? :

#列表每个元素都有个下标, get the element values from the list according to the subscript: list=["index1", "Index2", 1,2]print (list[1]) index2 #打印结果 # list element subscripts are calculated from 0, followed by 0,1,2,3 in order.    Print (list[2]) 1 #打印结果 # Prints the last value of the list: printed (List[-1]) 2 #打印结果 # Prints the second element of the list to the last element: print (list[1:]) ["Index2", Min] #打印结果 # Symbol: A fixed format for the list: before the start element position: After the last element of the final print position: After the position is not added, it represents the previous position from: Print to the last element of the list. #打印列表的第一个元素到倒数第二个元素: Print (list[0:-1]) ["Index1", "Index2", 1] #打印结果 # every 11 bits, prints the list element once: print (List[0::2]) ["Index1", 1] # Print results

Introduce the basic definition of the list, get the elements, the following is to introduce the list of the increase, delete, check, change:

Increase:

#首先定义一个列表: list=["Test", "book"] #在列表后追加元素: List.append ("Bike") print (list) ["Test", "book", "Bike"]# Append elements to an element in the list: List.insert (0, "bike") print (list) ["Bike", "Test", "book"] #打印结果

By deleting:

#定义一个列表: list=["Test", "book"] #删除列表中的 "test": List.remove ("Test") print (list) ["book"] #打印结果 # Delete the element labeled 3 in the list: List.pop (3) print (list) ["Test", +]

Check:

#定义一个列表: list=["Test", 1,1,2, "book"] #查找列表中名为test的元素: Print (List[list.index ("Test")) test #打印结果 #list.index ()  To get the subscript of an element, the list cannot print the element name directly, and it needs to be printed by subscript; #查找列表中名为1的元素的数量: Print (List.count (1)) 2 #打印结果 # View the length of the list: print (list, "\nlist length is : ", Len (list)) #len () to see the length of the string. ["Test", 1,1,2, "book"] #打印结果list length is:5 #打印结果

Change:

#定义一个列表: list=["Test", "book"] #将列表中第一个元素改为 "bike": list[0]= "Bike" print (list) ["Bike", and "book"] #打印列表 # will list "book "Bike": Modify=list.index ("book") list[modify]= "list" Print (list) ["Test", "bike"] #打印结果

Other operations:

#定义一个列表: list=["Test", "book"] #拷贝list, generate a new list2:list2=list.copy () #这个copy为浅拷贝, which is equivalent to a soft link inside Linux, when the elements in the list change , the elements in the List2 also change at the same time, #深拷贝, the deep copy is equivalent to the copy of Windows, the original list changes, List2 does not change: List2=list.deepcopy () #列表的反转输出: List.reverse () print ( list) [' Book ', 2, 1, ' Test '] #打印结果 # Merge: list=["Test",]list2=["book", 2,1, "Test"]list.extend (list2) print (l IST) [' Test ', ' book ', ' book ', 2,1, ' test '] #打印结果 # list nested: list=["Test", "book", ["book", 2,1, "Test"]]print (list) [' Test ', "," book ", [' book ', 2,1, ' Test ']] #打印结果 # Gets the list of elements in the list: print (List[-1][0]) book #打印结果 # list sort, int and STR strings cannot be performed: List=[3, 1,2,8]list.sort () print (list) [1, 2, 3, 8] #打印结果

Speaking of which, the list of related knowledge is basically over, the following is simply the tuple:

Tuples, in fact, is a list of read-only mode, tuples can not be modified, that is, only query, can not be added, deleted, change operation.

Definition of tuples:

R= ("Test", 3, "bike")

Tuples are similar to queries and lists, so this is not a demonstration.












Python list, tuples

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.